reduce

How can operations like map, filter and reverse can be defined in terms of a reduce?

假装没事ソ 提交于 2019-11-28 06:05:33
问题 In this blog entry, "CSP and transducers in JavaScript", the author states: First, we have to realise that many array (or other collection) operations like map , filter and reverse can be defined in terms of a reduce . My question is: How can operations like map, filter and reverse can be defined in terms of a reduce? Could you provide examples in Clojure? 回答1: This is true, if we don't care about laziness. In Clojure, map and filter are lazy, but reduce is eager. Not only is reverse not lazy

Why does reduce give a StackOverflowError in Clojure?

微笑、不失礼 提交于 2019-11-28 05:23:39
I'm trying to concatenate a Seq of Seqs. I can do it with apply concat . user=> (count (apply concat (repeat 3000 (repeat 3000 true)))) 9000000 However, from my limited knowledge, I would assume that the use of apply forces the lazy Seq to be realised, and that doesn't seem right for very large inputs. I'd rather do this lazily if I can. So I thought that using reduce would do the job. user=> (count (reduce concat (repeat 3000 (repeat 3000 true)))) But this results in StackOverflowError clojure.lang.RT.seq (RT.java:484) I'm surprised because I would have thought that the semantics of reduce

How does reduce function work?

て烟熏妆下的殇ゞ 提交于 2019-11-28 04:52:45
As far as I understand, the reduce function takes a list l and a function f . Then, it calls the function f on first two elements of the list and then repeatedly calls the function f with the next list element and the previous result. So, I define the following functions: The following function computes the factorial. def fact(n): if n == 0 or n == 1: return 1 return fact(n-1) * n def reduce_func(x,y): return fact(x) * fact(y) lst = [1, 3, 1] print reduce(reduce_func, lst) Now, shouldn't this give me ((1! * 3!) * 1!) = 6 ? But, instead it gives 720 . Why 720 ? It seems to take the factorial of

How to map/reduce/filter a Set in JavaScript?

牧云@^-^@ 提交于 2019-11-28 04:36:27
Is there any way to map / reduce / filter /etc a Set in JavaScript or will I have to write my own? Here's some sensible Set.prototype extensions Set.prototype.map = function map(f) { var newSet = new Set(); for (var v of this.values()) newSet.add(f(v)); return newSet; }; Set.prototype.reduce = function(f,initial) { var result = initial; for (var v of this) result = f(result, v); return result; }; Set.prototype.filter = function filter(f) { var newSet = new Set(); for (var v of this) if(f(v)) newSet.add(v); return newSet; }; Set.prototype.every = function every(f) { for (var v of this) if (!f(v

Understanding treeReduce() in Spark

情到浓时终转凉″ 提交于 2019-11-28 03:42:52
问题 You can see the implementation here: https://github.com/apache/spark/blob/ffa05c84fe75663fc33f3d954d1cb1e084ab3280/python/pyspark/rdd.py#L804 How does it different from the 'normal' reduce function? What does it mean depth = 2 ? I don't want that the reducer function will pass linearly on the partitions, but reduce each available pairs first, and then will iterate like that until i have only one pair and reduce it to 1, as shown in the picture: Does treeReduce achieve that? 回答1: Standard

ES6 - Removing duplicates from array of objects

不问归期 提交于 2019-11-27 23:36:12
问题 Assuming an array of objects as follows: const listOfTags = [ {id: 1, label: "Hello", color: "red", sorting: 0}, {id: 2, label: "World", color: "green", sorting: 1}, {id: 3, label: "Hello", color: "blue", sorting: 4}, {id: 4, label: "Sunshine", color: "yellow", sorting: 5}, {id: 5, label: "Hello", color: "red", sorting: 6}, ] A duplicate entry would be if label and color are the same. In this case Objects with id = 1 and id = 5 are duplicates. How can I filter this array and remove duplicates

MPI Get Processor with Minimum value

拟墨画扇 提交于 2019-11-27 19:51:08
In MPI, I am doing a reduce operation(minimum) on a value. This works fine, but how do I grab the processor number that the minimum came from and solicit that processor for more information(or send the additional data with the reduce operation)? Jonathan Dursi If you don't mind paring each value locally with an integer index (filled in this case with the value of the local rank), you can use the MPI_MINLOC or MPI_MAXLOC builtin operations for reduce; or it's fairly easy to write your own MPI reduction operator to include things like multiple indices, etcc Updated to add: With the builtin

Merging more than 2 dataframes in R by rownames

依然范特西╮ 提交于 2019-11-27 18:31:22
I gather data from 4 df's and would like to merge them by rownames. I am looking for an efficient way to do this. This is a simplified version of the data I have. df1 <- data.frame(N= sample(seq(9, 27, 0.5), 40, replace= T), P= sample(seq(0.3, 4, 0.1), 40, replace= T), C= sample(seq(400, 500, 1), 40, replace= T)) df2 <- data.frame(origin= sample(c("A", "B", "C", "D", "E"), 40, replace= T), foo1= sample(c(T, F), 40, replace= T), X= sample(seq(145600, 148300, 100), 40, replace= T), Y= sample(seq(349800, 398600, 100), 40, replace= T)) df3 <- matrix(sample(seq(0, 1, 0.01), 40), 40, 100) df4 <-

How does one reduce a list of boolean values in Common Lisp?

别等时光非礼了梦想. 提交于 2019-11-27 17:51:06
问题 Given a list of values, I want to reduce the list to T if all the elements are not NIL, NIL if not. This gives me an error: (apply #'and (get-some-list)) As does this: (reduce #'and (get-some-list)) This is the best I've come up with: [11]> (defun my-and (x y) (and x y)) MY-AND [12]> (reduce #'my-and '(T T T T T)) T [13]> (reduce #'my-and '(T T T T NIL)) NIL Why is "#'and" invalid? Is there a more idiomatic way to do this in Common Lisp? 回答1: #'and is invalid because and is a macro, not a

What is the 'pythonic' equivalent to the 'fold' function from functional programming?

和自甴很熟 提交于 2019-11-27 17:46:02
What is the most idiomatic way to achieve something like the following, in Haskell: foldl (+) 0 [1,2,3,4,5] --> 15 Or its equivalent in Ruby: [1,2,3,4,5].inject(0) {|m,x| m + x} #> 15 Obviously, Python provides the reduce function, which is an implementation of fold, exactly as above, however, I was told that the 'pythonic' way of programming was to avoid lambda terms and higher-order functions, preferring list-comprehensions where possible. Therefore, is there a preferred way of folding a list, or list-like structure in Python that isn't the reduce function, or is reduce the idiomatic way of