fold

Folding, function composition, monads, and laziness, oh my?

笑着哭i 提交于 2019-11-30 06:39:15
I am puzzled. I can write this: import Control.Monad main = print $ head $ (foldr (.) id [f, g]) [3] where f = (1:) g = undefined and the output is 1 . That makes sense, because it reduces to: main = print $ head $ ((1:) . undefined . id) [3] main = print $ head $ (1:) ((undefined . id) [3]) main = print $ head $ 1 : ((undefined . id) [3]) main = print $ 1 But if I use a vaguely similar monadic technique, it doesn't work the same: import Control.Monad main = print $ (foldr (<=<) return [f, g]) 3 where f = const Nothing g = undefined This hits prelude.Undefined . Which is odd, because I would

foldRight on infinite lazy structure

拜拜、爱过 提交于 2019-11-30 06:28:34
According to http://en.wikipedia.org/wiki/Fold_(higher-order_function ), a right fold can operate on infinite lists if the full list does not have to be evaluated. This can be seen in action in haskell: Prelude> take 5 (foldr (:) [] [1 ..]) [1,2,3,4,5] This does not seem to work well in scala for streams: Stream.from(1).foldRight(Stream.empty[Int])( (i, s) => i #:: s).take(5) // StackOverflowError or on iterators: Iterator.from(1).foldRight(Iterator.empty: Iterator[Int]){ (i, it) => Iterator.single(i) ++ it }.take(5) // OutOfMemoryError: Java heap space Is there a practical solution to achieve

foldLeft or foldRight equivalent in Spark?

纵然是瞬间 提交于 2019-11-30 03:20:44
问题 In Spark's RDDs and DStreams we have the 'reduce' function for transforming an entire RDD into one element. However the reduce function takes (T,T) => T However if we want to reduce a List in Scala we can use foldLeft or foldRight which takes type (B)( (B,A) => B) This is very useful because you start folding with a type other then what is in your list. Is there a way in Spark to do something similar? Where I can start with a value that is of different type then the elements in the RDD itself

What is basic difference between fold and reduce in Kotlin? When to use which?

对着背影说爱祢 提交于 2019-11-30 00:12:36
I am going through basics of Kotlin and I am pretty confused with this both functions fold() and reduce() in Kotlin, can anyone give me a concrete example which distinguishes both of them? fold takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters. For example, take the following code that calculates the sum of a list of integers: listOf(1, 2, 3).fold(0) { sum, element -> sum + element } The first call to the lambda will be with parameters 0 and 1 . Having the ability to pass in an

difference between foldLeft and reduceLeft in Scala

人盡茶涼 提交于 2019-11-29 19:03:25
I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference ? Any specific reason to have two methods with similar functionality? Few things to mention here, before giving the actual answer: Your question doesn't have anything to do with left , it's rather about the difference between reducing and folding The difference is not the implementation at all, just look at the signatures. The question doesn't have

Is it possible to use continuations to make foldRight tail recursive?

血红的双手。 提交于 2019-11-29 18:26:45
问题 The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x, foldBack(xs, acc)(f)) case Nil => acc } } can be made tail recursive by doing this: def foldCont[T,U](list: List[T], acc: U)(f: (T, U) => U): U = { @annotation.tailrec def loop(l: List[T], k: (U) => U): U = { l match { case x :: xs => loop(xs, (racc => k(f(x,

Scheme - sum the squares of even-valued elements in a list

有些话、适合烂在心里 提交于 2019-11-29 16:41:50
I want to be able to sum the squares of the even elements in the list, however my current code only sums the elements, not the squares. Does anyone know of any modifications that can be made to make this to sum the squares of the even-valued elements in the list? (define (sum elemList) (if (null? elemList) 0 (+ (car elemList) (sum (cdr elemList))) ) ) My input would be: (sum-evens (list 1 2 3 4)) Output would be: 20 Which is (2*2) + (4*4) . If possible, it would be good to see both a recursive and iterative solution. Any ideas? There are two possibilities, either we implement the recursion

Explanation of lists:fold function

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 16:25:19
I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case string:to_float(N) of %returning {error, no_float} where there is no float avaiable {error,no_float} ->

Sml folding a tree

女生的网名这么多〃 提交于 2019-11-29 12:26:53
I am trying to get the product of a tree by using the fold function so far this is what I have. I am confused on how to use the fold method while transversing the tree datatype 'a bin_tree = Leaf of 'a | Node of 'a bin_tree * 'a bin_tree fun treefold g z Empty = z | treefold g z (Node (l, x, r)) = g(x, g(treefold g z l, treefold g z r) First, some pointers about what isn't quite in order in your attempt. The base case of your treefold function matches against the value constructor Empty , but you don't define the bin_tree datatype to include an Empty value constructor. As John Coleman points

Example of the difference between List.fold and List.foldBack

偶尔善良 提交于 2019-11-29 09:23:45
My understanding of the difference between List.fold and List.foldBack is that foldBack iterates over its list in a reverse order. Both functions accumulate a result from the items in the list. I'm having trouble coming up with a good example where it is preferable to foldBack over a list. In the examples I have come up with, the results are the same for both fold and foldBack, if the function logic does the same thing. [<Fact>] let ``List.foldBack accumulating a value from the right to the left``() = let list = [1..5] let fFoldBack x acc = acc - x let fFold acc x = acc - x let foldBackResult