Questions tagged [fold]

In functional programming, a fold, also known variously as reduction, accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value

In functional programming, a fold, also known variously as , accumulation, or catamorphism, is a type of higher-order function that recursively applies a transformation to a data structure, "collapsing" it to a summary value.

1159 questions
11
votes
5 answers

foldl / foldr query

I'm a beginner at Haskell, and even after reading several explanations of foldr/foldl, I can't understand why I'm getting different results below. What is the explanation? Prelude> foldl (\_ -> (+1)) 0 [1,2,3] 4 Prelude> foldr (\_ -> (+1)) 0…
Frank
  • 4,341
  • 8
  • 41
  • 57
11
votes
1 answer

Is there an efficient, lazy way to fuse foldMap with traverse?

A recent proposal on the Haskell libraries mailing list led me to consider the following: ft :: (Applicative f, Monoid m, Traversable t) -> (b -> m) -> (a -> f b) -> t a -> f m ft f g xs = foldMap f <$> traverse g xs I noticed that the…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
11
votes
3 answers

How does the fold action work in Spark?

Below I have a Scala example of a Spark fold action: val rdd1 = sc.parallelize(List(1,2,3,4,5), 3) rdd1.fold(5)(_ + _) This produces the output 35. Can somebody explain in detail how this output gets computed?
thedevd
  • 683
  • 11
  • 26
11
votes
1 answer

Tail recursive fold on a binary tree in Scala

I am trying to find a tail recursive fold function for a binary tree. Given the following definitions: // From the book "Functional Programming in Scala", page 45 sealed trait Tree[+A] case class Leaf[A](value: A) extends Tree[A] case class…
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
11
votes
2 answers

Ruby - test each array element, get one result

I want a one-liner to return true/false, that tests each element in an array for whether it's an Integer or not. So if any element in the array is not an Integer, it should return false, else true. Here's my try: >> ([2,1,4].map {|x| (x.is_a?…
oaklodge
  • 722
  • 6
  • 19
11
votes
1 answer

Accumulate vs fold vs reduce vs compress

Are the functions accumulate, fold, reduce, and compress synonyms?
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
11
votes
2 answers

Multiple folds in one pass using generic tuple function

How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai, one element of type b, and combines each of the elements into a new tuple of ai: That is the…
Clinton
  • 22,361
  • 15
  • 67
  • 163
10
votes
4 answers

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

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,…
huynhjl
  • 41,520
  • 14
  • 105
  • 158
10
votes
2 answers

FoldRight over Infinite Structures in Scala using Trampolines

Let's start with a straightforward definition of foldRight: def foldRight[T, U](base: U)(f: (T, => U) => U)(as: Seq[T]): U = { as match { case Nil => base case head +: next => f(head, foldRight(base)(f)(next)) } } One of the advantages…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
10
votes
6 answers

Scala foldLeft while some conditions are true

How to emulate following behavior in Scala? i.e. keep folding while some certain conditions on the accumulator are met. def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B For example scala> val seq = Seq(1, 2, 3, 4) seq: Seq[Int] =…
ntviet18
  • 752
  • 1
  • 10
  • 26
10
votes
2 answers

Haskell's foldr equivalent in Java 8

We are used to foldr in Haskell where you take (for example, using Java syntax) a List and you return whatever type you want (, List, etc.). For example in Haskell, this function that takes a List and return another List
Nico
  • 858
  • 2
  • 11
  • 27
10
votes
1 answer

foldLeft or foldRight equivalent in Spark?

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…
zunior
  • 841
  • 2
  • 13
  • 24
10
votes
4 answers

How to implement zip with foldl (in an eager language)

A Clojure programmer I know recently remarked that it's possible to implement lots of sequence functions in terms of Clojure's reduce (which is Haskell's foldl'), but that regrettably there's no way to implement (map list xs ys) (which is Haskell's…
amalloy
  • 89,153
  • 8
  • 140
  • 205
10
votes
3 answers

Does haskell's foldr always take a two-parameter lambda?

Haskell newb here I'm working on this problem in haskell: (**) Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be…
Mark Karavan
  • 2,654
  • 1
  • 18
  • 38
10
votes
2 answers

What is the most general way to compute the depth of a tree with something like a fold?

What minimal (most general) information is required to compute depth of a Data.Tree? Is instance of a Data.Foldable sufficient? I initially tried to fold a Tree and got stuck trying to find right Monoid similar to Max. Something tells me that since…
sevo
  • 4,559
  • 1
  • 15
  • 31