reduce

Composing monad actions with folds

萝らか妹 提交于 2019-12-01 21:28:19
问题 Let's take a function of type (Monad m) => a -> m a . For example: ghci> let f x = Just (x+1) I'd like to be able to apply it any number of times. The first thing I tried was ghci> let times n f = foldr (>=>) return $ replicate n f The problem is that it won't work for large n : ghci> 3 `times` f $ 1 Just 4 ghci> 1000000 `times` f $ 1 Just *** Exception: stack overflow It doesn't work also the other way: ghci> let timesl n f = foldl' (<=<) return $ replicate n f ghci> 3 `timesl` f $ 1 Just 4

What is the difference between combining array by using reduce or joined?

霸气de小男生 提交于 2019-12-01 21:08:41
Consider the following array -of strings-: let arrayStrings = ["H", "e", "l", "l", "o"] For combining its elements (to get "Hello" as single String), we could: reduce it: let reducedString = arrayStrings.reduce("", { $0 + $1 }) // "Hello" Or join it: let joinedString = arrayStrings.joined() // "Hello" Both would return "Hello" String as output. However, what is the logic to keep in mind to determine what is the better choice for such a process? What is the difference when comparing based on the performance? There are two reasons why joined is a better choice than reduce : Readability If you

第十章 Scala 容器基础(二十):使用reduce和fold方法遍历集合的所有元素

拟墨画扇 提交于 2019-12-01 19:44:36
Problem 你想要遍历有序集合的所有元素,并且随着你对集合元素的遍历,对比两个相邻的元素 Solution 使用 reduceLeft, foldLeft, reduceRight, foldRight来遍历集合的元素,你的方法作用在相邻的两个元素上,从第一次要遍历的两个相邻元素开始,把你的方法作用在这两个元素上得到返回值,然后把你的方法继续作用在返回值和集合中第三要遍历的元素得到的返回值,再继续和第四个要遍历的元素作用。。。直到遍历完最后一个元素为止: scala> val a = Array(12, 6, 15, 2, 20, 9) a: Array[Int] = Array(12, 6, 15, 2, 20, 9) scala> a.reduceLeft(_ + _) res32: Int = 64 这个例子是这样的:12+6=18, 18+15=33, 33+2=35, 35+20=55, 55+9=64,就是对集合的所有元素求和 接下来你会看到如何使用reduceLeft来计算集合元素的乘积和求最大最小值: scala> a.reduceLeft(_ * _) res33: Int = 388800 scala> a.reduceLeft(_ min _) res34: Int = 2 scala> a.reduceLeft(_ max _) res35: Int =

Clojure: summing values in a collection of maps

自闭症网瘾萝莉.ら 提交于 2019-12-01 19:07:00
I am trying to sum up values of a collection of maps by their common keys. I have this snippet: (def data [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3}] (for [xs data] (map xs [:a :b])) ((1 2) (1 2)) Final result should be ==> (2 4) Basically, I have a list of maps. Then I perform a list of comprehension to take only the keys I need. My question now is how can I now sum up those values? I tried to use "reduce" but it works only over sequences, not over collections. Thanks. ===EDIT==== Using the suggestion from Joost I came out with this: (apply merge-with + (for [x data] (select-keys x [:col0 :col1 :col2

Java mapToInt vs Reduce with map

妖精的绣舞 提交于 2019-12-01 17:46:48
I've been reading up on reduce and have just found out that there is a 3 argument version that can essentially perform a map reduce like this: String[] strarr = {"abc", "defg", "vwxyz"}; System.out.println(Arrays.stream(strarr).reduce(0, (l, s) -> l + s.length(), (s1, s2) -> s1 + s2)); However I can't see the advantage of this over a mapToInt with a reduce. System.out.println(Arrays.stream(strarr).mapToInt(s -> s.length()).reduce(0, (s1, s2) -> s1 + s2)); Both produce the correct answer of 12, and both appear to work fine in parallel. Is one better than the other, and if so, why? Is one better

Are Clojure transducers eager?

China☆狼群 提交于 2019-12-01 17:20:44
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 . So then we see a number of implementations of this in Clojure aren't lazy, they are eager: user> (defn eager-map [f coll] (reduce (fn [acc v] (conj acc (f v))) [] coll)) #'user/eager-map user> (eager-map inc (range 10)) [1 2 3 4 5 6 7 8 9 10] My question is, are Clojure transducers eager? Beyamor Transducers are very simple functions - they don't have a notion of laziness or,

Are Clojure transducers eager?

邮差的信 提交于 2019-12-01 16:19:58
问题 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 . So then we see a number of implementations of this in Clojure aren't lazy, they are eager: user> (defn eager-map [f coll] (reduce (fn [acc v] (conj acc (f v))) [] coll)) #'user/eager-map user> (eager-map inc (range 10)) [1 2 3 4 5 6 7 8 9 10] My question is, are Clojure

Reduce in Cuda for arbitrary number of elements

旧街凉风 提交于 2019-12-01 14:17:21
How can I implement version 7 of the code given in the following link: http://www.cuvilib.com/Reduction.pdf for an input array whose size is an arbitrary number, in other words, not a power of 2? Version 7 already handles an arbitrary number of elements. Perhaps instead of referring to the cuvilib link, you should look at the link to the relevant NVIDIA CUDA reduction sample . It includes essentially the pdf file you are using, but also sample codes that implement reductions 1 through 7 (labelled reduce0 through reduce6 ) If you study the description of the reduction 7 in the document, you'll

Reduce in Cuda for arbitrary number of elements

♀尐吖头ヾ 提交于 2019-12-01 12:14:55
问题 How can I implement version 7 of the code given in the following link: http://www.cuvilib.com/Reduction.pdf for an input array whose size is an arbitrary number, in other words, not a power of 2? 回答1: Version 7 already handles an arbitrary number of elements. Perhaps instead of referring to the cuvilib link, you should look at the link to the relevant NVIDIA CUDA reduction sample. It includes essentially the pdf file you are using, but also sample codes that implement reductions 1 through 7

Convert reduce function to work with IE

你说的曾经没有我的故事 提交于 2019-12-01 10:58:56
Alright, so I had some help a couple months ago with coming up with a solution to keep count of the elements in an array: Loop through multiple array and keep count of each element This solution worked perfectly for me until I realized that it's using ES6 which is not supported by IE 11 . I've tried to convert it to using functions instead of the arrow function so that it'll work across all browsers but am having some issues. Here is the current code that does not work in IE: var b = data.reduce((acc, cur) => { cur.ProductHandlingTypes.map(({ Name }) => Name).forEach(n => acc[n] = (acc[n] || 0