fold

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

时光怂恿深爱的人放手 提交于 2019-11-26 19:13:23
问题 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

Why is the fold action necessary in Spark?

谁说胖子不能爱 提交于 2019-11-26 16:19:09
问题 I've a silly question involving fold and reduce in PySpark . I understand the difference between these two methods, but, if both need that the applied function is a commutative monoid, I cannot figure out an example in which fold cannot be substituted by reduce`. Besides, in the PySpark implementation of fold it is used acc = op(obj, acc) , why this operation order is used instead of acc = op(acc, obj) ? (this second order sounds more closed to a leftFold to me) Cheers Tomas 回答1: Empty RDD It

Why is foldl defined in a strange way in Racket?

ⅰ亾dé卋堺 提交于 2019-11-26 16:04:55
问题 In Haskell, like in many other functional languages, the function foldl is defined such that, for example, foldl (-) 0 [1,2,3,4] = -10 . This is OK, because foldl (-) 0 [1, 2,3,4] is, by definition, ((((0 - 1) - 2) - 3) - 4) . But, in Racket, (foldl - 0 '(1 2 3 4)) is 2, because Racket "intelligently" calculates like this: (4 - (3 - (2 - (1 - 0)))) , which indeed is 2. Of course, if we define auxiliary function flip, like this: (define (flip bin-fn) (lambda (x y) (bin-fn y x))) then we could

Scala : fold vs foldLeft

好久不见. 提交于 2019-11-26 16:04:54
问题 I am trying to understand how fold and foldLeft and the respective reduce and reduceLeft work. I used fold and foldLeft as my example scala> val r = List((ArrayBuffer(1, 2, 3, 4),10)) scala> r.foldLeft(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1) scala> res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5) scala> r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1) <console>:11: error: value _1 is not a member of Serializable with Equals r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1)

An example of a Foldable which is not a Functor (or not Traversable)?

我只是一个虾纸丫 提交于 2019-11-26 15:44:38
问题 A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says A Foldable type is also a container (although the class does not technically require Functor , interesting Foldable s are all Functor s). So is there an example of a Foldable which is not naturally a Functor or a Traversable ? (which perhaps the Haskell wiki page missed :-) ) 回答1: Here's a fully parametric example: data Weird a = Weird a (a -> a) instance Foldable Weird

foldl versus foldr behavior with infinite lists

帅比萌擦擦* 提交于 2019-11-26 15:36:47
The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied. I rewrote it using foldl: myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldl step False list where step acc item = p item || acc (Note that the arguments to the step function are correctly reversed.) However, it no longer stops processing infinite lists. I attempted to trace the function's execution as in Apocalisp's answer : myAny even [1..] foldl step False [1..] step (foldl step False [2..]) 1 even 1 || (foldl step False [2..]) False || (foldl step False

Writing foldl using foldr

早过忘川 提交于 2019-11-26 14:54:41
In Real World Haskell , Chapter 4. on Functional Programming : Write foldl with foldr: -- file: ch04/Fold.hs myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a = g (f a x) The above code confused me a lot, and somebody called dps rewrote it with a meaningful name to make it a bit clearer: myFoldl stepL zeroL xs = (foldr stepR id xs) zeroL where stepR lastL accR accInitL = accR (stepL accInitL lastL) Somebody else, Jef G, then did an excellent job by providing an example and showing the underlying mechanism step by step: myFoldl (+) 0 [1, 2, 3] =

Using Haskell&#39;s map function to calculate the sum of a list

China☆狼群 提交于 2019-11-26 11:38:03
问题 Haskell addm::[Int]->Int addm (x:xs) = sum(x:xs) I was able to achieve to get a sum of a list using sum function but is it possible to get the sum of a list using map function? Also what the use of map function? 回答1: You can't really use map to sum up a list, because map treats each list element independently from the others. You can use map for example to increment each value in a list like in map (+1) [1,2,3,4] -- gives [2,3,4,5] Another way to implement your addm would be to use foldl:

Difference between fold and foldLeft or foldRight?

让人想犯罪 __ 提交于 2019-11-26 10:25:13
问题 NOTE: I am on Scala 2.8—can that be a problem? Why can\'t I use the fold function the same way as foldLeft or foldRight ? In the Set scaladoc it says that: The result of folding may only be a supertype of this parallel collection\'s type parameter T . But I see no type parameter T in the function signature: def fold [A1 >: A] (z: A1)(op: (A1, A1) ⇒ A1): A1 What is the difference between the foldLeft-Right and fold , and how do I use the latter? EDIT: For example how would I write a fold to

Functional programming, Scala map and fold left [closed]

▼魔方 西西 提交于 2019-11-26 10:06:28
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What are some good tutorials on fold left? Original question, restored from deletion to provide context for other answers: I am trying