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
28
votes
4 answers

Why does foldr use a helper function?

In explaining foldr to Haskell newbies, the canonical definition is foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = f x (foldr f z xs) But in GHC.Base, foldr is defined as foldr k z = go where …
Matt Adams
  • 709
  • 4
  • 11
27
votes
5 answers

How to fold STL container?

I need an analog of Haskell's foldl function to fold any STL containers. Expected signature is like following: template Iterator, FoldingFunction, Result Result foldl( Iterator begin, Iterator end, FoldingFunction f, Result…
Andrey
  • 3,667
  • 6
  • 27
  • 36
27
votes
5 answers

Applying logical and to list of boolean values

Consider the following list of Boolean values in Scala List(true, false, false, true) How would you using either foldRight or foldLeft emulate the function of performing a logical AND on all of the values within the list?
user3277752
  • 569
  • 1
  • 9
  • 17
26
votes
1 answer

What is the purpose of trailing lambda syntax (Kotlin)?

Passing a lambda to the last parameter In Kotlin, there is a convention that if the last parameter of a function accepts a function, a lambda expression that is passed as the corresponding argument can be placed outside the parentheses: val…
vkelman
  • 1,501
  • 1
  • 15
  • 25
26
votes
7 answers

C++17 Variadic Template Folding

I don't understand why this doesn't work. Could someone who understands templates and variadic expression folding explain what is going on and give a solution that does work? #include #include template void…
nickeb96
  • 789
  • 1
  • 10
  • 16
23
votes
2 answers

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

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)…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
23
votes
2 answers

Is foldl ever preferable to its strict cousin, foldl'?

Haskell has two left fold functions for lists: foldl, and a "strict" version, foldl'. The problem with the non-strict foldl is that it builds a tower of thunks: foldl (+) 0 [1..5] --> ((((0 + 1) + 2) + 3) + 4) + 5 --> 15 This wastes memory,…
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
23
votes
3 answers

Is implementing the words function possible without a postprocessing step after folding?

Real World Haskell, chapter 4, page 98 of the printed version asks if words can be implemented using folds, and this is my question too: Is it possible? If not, why? If it is, how? I came up with the following, which is based on the idea that each…
Enlico
  • 23,259
  • 6
  • 48
  • 102
23
votes
3 answers

Are there non-trivial Foldable or Traversable instances that don't look like containers?

There are lots of functors that look like containers (lists, sequences, maps, etc.), and many others that don't (state transformers, IO, parsers, etc.). I've not yet seen any non-trivial Foldable or Traversable instances that don't look like…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
22
votes
7 answers

Implement zip using foldr

I'm currently on chapter 4 of Real World Haskell, and I'm trying to wrap my head around implementing foldl in terms of foldr. (Here's their code:) myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a =…
itsadok
  • 28,822
  • 30
  • 126
  • 171
21
votes
2 answers

Visual Code, Fold Comments

Visual Studio Code 1.24.1 While I was working on something today. It prompted me to do an update which I did (Update was to 1.24.1). I'm not sure if I hit a shortcut accidentally at about this same time or if this was caused by the update. But I…
James
  • 211
  • 1
  • 2
  • 4
19
votes
1 answer

foldRight on infinite lazy structure

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

How would you define map and filter using foldr in Haskell?

I'm doing a bit of self study on functional languages (currently using Haskell). I came across a Haskell based assignment which requires defining map and filter in terms of foldr. For the life of me I'm not fully understanding how to go about…
klactose
  • 1,190
  • 2
  • 9
  • 26
19
votes
9 answers

How to break out from a fold function in haskell when the accumulator met a certain condition?

I'm calculating the sum of a list after applying someFunction to every element of it like so: sum (map someFunction myList) someFunction is very resource heavy so to optimise it I want to stop calculating the sum if it goes above a certain…
Balázs Sáros
  • 495
  • 6
  • 12
19
votes
6 answers

Java 8: stop reduction operation from examining all Stream elements

I am trying to understand if there is a way to terminate reduction operation without examining the whole stream and I cannot figure out a way. The use-case is roughly as follows: let there be a long list of Integers which needs to be folded into an…
quantum
  • 3,000
  • 5
  • 41
  • 56
1 2
3
77 78