reduce

“contextual closure type expects 2 arguments” error when using reduce in Swift 4

China☆狼群 提交于 2019-12-03 13:31:43
问题 The following code compiles in Swift 3 extension Array where Element: Equatable { var removeDuplicate: [Element] { return reduce([]){ $0.0.contains($0.1) ? $0.0 : $0.0 + [$0.1] } } } but produces the error error: contextual closure type '(_, _) -> _' expects 2 arguments, but 1 was used in closure body in Swift 4. How to convert this code to be compiled in Swift 4? 回答1: The closure passed to reduce takes 2 parameters, e.g. $0 and $1 in the shorthand notation: extension Array where Element:

Reduce Hash Values

冷暖自知 提交于 2019-12-03 08:16:39
问题 I am having trouble with the syntax for reduce. I have a hash of the following format: H = {"Key1" => 1, "Key2" => 2} I would like to use reduce to find the sum of the values in this function. Something Like H.reduce(0) {|memo, elem| memo+=elem} I know this is wrong. I dont understand how I can make elem the value of the hash. 回答1: Use Enumerable#reduce, if you're ok with getting nil if the hash happens to be empty: H.values.reduce(:+) # => 3 Hash.new.values.reduce(:+) # => nil To safely get

Mapping values from two array in Ruby

房东的猫 提交于 2019-12-03 06:47:52
问题 I'm wondering if there's a way to do what I can do below with Python, in Ruby: sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data)) I have two arrays of equal sizes with the weights and data but I can't seem to find a function similar to map in Ruby, reduce I have working. 回答1: @Michiel de Mare Your Ruby 1.9 example can be shortened a bit further: weights.zip(data).map(:*).reduce(:+) Also note that in Ruby 1.8, if you require ActiveSupport (from Rails) you can use: weights

python: union keys from multiple dictionary?

五迷三道 提交于 2019-12-03 06:39:07
I have 5 dictionaries and I want a union of their keys. alldict = [dict1, dict2, dict3, dict4, dict5] I tried allkey = reduce(lambda x, y: set(x.keys()).union(y.keys()), alldict) but it gave me an error AttributeError: 'set' object has no attribute 'keys' Am I doing it wrong ? I using normal forloop but I wonder why the above code didn't work. Your solution works for the first two elements in the list, but then dict1 and dict2 got reduced into a set and that set is put into your lambda as the x . So now x does not have the method keys() anymore. The solution is to make x be a set from the very

Spark: difference of semantics between reduce and reduceByKey

╄→尐↘猪︶ㄣ 提交于 2019-12-03 06:02:35
In Spark's documentation, it says that RDDs method reduce requires a associative AND commutative binary function. However, the method reduceByKey ONLY requires an associative binary function. sc.textFile("file4kB", 4) I did some tests, and apparently it's the behavior I get. Why this difference? Why does reduceByKey ensure the binary function is always applied in certain order (to accommodate for the lack of commutativity) when reduce does not? Example, if a load some (small) text with 4 partitions (minimum): val r = sc.textFile("file4k", 4) then: r.reduce(_ + _) returns a string where parts

Compose example in Paul Graham's ANSI Common Lisp

百般思念 提交于 2019-12-03 05:41:36
Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is: (mapcar (compose #'list #'round #'sqrt) '(4 9 16 25)) The output is: ((2) (3) (4) (5)) Line 2 and 6 look

JavaScript array .reduce with async/await

柔情痞子 提交于 2019-12-03 05:32:02
问题 Seem to be having some issues incorporating async/await with .reduce(), like so: const data = await bodies.reduce(async(accum, current, index) => { const methodName = methods[index] const method = this[methodName] if (methodName == 'foo') { current.cover = await this.store(current.cover, id) console.log(current) return { ...accum, ...current } } return { ...accum, ...method(current.data) } }, {}) console.log(data) The data object is logged before the this.store completes... I know you can

Use global variable in reudcer class

£可爱£侵袭症+ 提交于 2019-12-03 05:06:12
I need to use global variable in my mapreduce program how to set it in following code and use global variable in reducer. public class tfidf { public static tfidfMap.............. { } public static tfidfReduce............. { } public static void main(String args[]) { Configuration conf=new Configuration(); conf.set("",""); } } Template code could look something like this (Reducer not shown but is the same principal) import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.io.LongWritable; import org.apache

Reducing/Grouping an array in Javascript

帅比萌擦擦* 提交于 2019-12-02 23:40:57
问题 Based on this example, I want to group by object in a slightly other way. The outcome should be as follows: [{ key: "audi" items: [ { "make": "audi", "model": "r8", "year": "2012" }, { "make": "audi", "model": "rs5", "year": "2013" } ] }, ... ] How can I achieve that? The following code I wrote doesn't do what I want: reduce(function (r, a) { r[a.art] = {key: r[a.art], items: []} || []; r[a.art].items.push(a); return r; }, Object.create(null)); 回答1: You could use a hash table for grouping by

Add integer values present in an array of custom objects

喜你入骨 提交于 2019-12-02 23:35:36
问题 I have a struct called Offering as shown below. I need to add all the amount present in the array of Offering "offerings" using reduce and/or map. Please help me. public struct Offering: Codable { public let company: String public let amount: Int public let location: String } var offerings = [Offering]() 回答1: This can be done with reduce in a one-liner: let sum = offerings.reduce(0, { $0 + $1.amount }) $0 represents the partial result (i.e., what's been accumulated so far) and $1 is the