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
18
votes
2 answers

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

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…
Mike Coxeter
  • 589
  • 1
  • 6
  • 18
18
votes
5 answers

python reduce to find the union of sets

I am trying to find the union of set of sets. Specifically I want the union of the list of nodes for each key in the dictionary of networkx graphs called periodic_gs. I would like to use the reduce function as it seems reasonable to take the union…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
17
votes
3 answers

Scala Vector fold syntax (/: and :\ and /:\)

Can someone provide some examples for how /: :\ and /:\ Actually get used? I assume they're shortcuts to the reduce / fold methods, but there's no examples on how they actually get used in the Scala docs, and they're impossible to google / search…
chbrown
  • 11,865
  • 2
  • 52
  • 60
17
votes
1 answer

Why does foldl' use a lot of RAM with complex data structures?

Lazy fold uses a lot of RAM. In Data.List, foldl' provides a left fold that uses strict evaluation. For example, the following computes the sum of 10 million zeros with little increase in RAM usage. sum0 = foldl' (+) 0 (replicate 10000000…
W. Zhu
  • 755
  • 6
  • 16
17
votes
4 answers

Can't perform I/O in foldr?

I have a Data.Map structure that maps Strings to Stringss. For whatever reason, I want to print the contents of the map in the format key: value using foldrWithKey, like so: M.foldrWithKey (\k v b -> putStrLn (k++": "++v++"\n")) (return ())…
user500944
17
votes
6 answers

foldr and foldl further explanations and examples

I've looked at different folds and folding in general as well as a few others and they explain it fairly well. I'm still having trouble on how a lambda would work in this case. foldr (\y ys -> ys ++ [y]) [] [1,2,3] Could someone go through that…
Matt
  • 7,049
  • 7
  • 50
  • 77
17
votes
4 answers

When is a composition of catamorphisms a catamorphism?

From page 3 of http://research.microsoft.com/en-us/um/people/emeijer/Papers/meijer94more.pdf: it is not true in general that catamorphisms are closed under composition Under what conditions do catamorphisms compose to a catamorphism? More…
16
votes
2 answers

Fold expression with comma operator and variadic template parameter pack

#include using namespace std; template void output_argus(Args&&... args) { ((cout << args << '\n'), ...); // #1 (... , (cout << args << '\n')); // #2 } int main() { output_argus(1, "test",…
Ke Zhang
  • 937
  • 1
  • 10
  • 24
16
votes
6 answers

Real world examples of using reduceRight in JavaScript

A while ago, I posted a question on StackOverflow showing that the native implementation of reduceRight in JavaScript is annoying. Hence, I created a Haskell-style foldr function as a remedy: function foldr(array, callback, initial) { var length…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
16
votes
2 answers

Practical use of `foldl`

Today when I was working on one little script I used foldl instead of foldl'. I got stack overflow, so I imported Data.List (foldl') and was happy with this. And this is my default workflow with foldl. Just use foldl' when lazy version falls to…
d12frosted
  • 1,280
  • 12
  • 33
16
votes
4 answers

Why does Haskell's foldr NOT stackoverflow while the same Scala implementation does?

I am reading FP in Scala. Exercise 3.10 says that foldRight overflows (See images below). As far as I know , however foldr in Haskell does not. http://www.haskell.org/haskellwiki/ -- if the list is empty, the result is the initial value z; else --…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
16
votes
2 answers

Foldr/Foldl for free when Tree is implementing Foldable foldMap?

I am a beginner at Haskell and learning from "Learn You a Haskell". There's something I don't understand about the Tree implementation of Foldable. instance F.Foldable Tree where foldMap f Empty = mempty foldMap f (Node x l r) =…
Roely de Vries
  • 213
  • 1
  • 4
16
votes
6 answers

Code folding is not saved in my vimrc

I added the following code to my .vimrc: " save and restore folds when a file is closed and re-opened autocmd BufWinLeave *.* mkview autocmd BufWinEnter *.* silent loadview HTML and CSS documents save and restore their folds but code folding is…
alexchenco
  • 53,565
  • 76
  • 241
  • 413
15
votes
5 answers

Please explain in the simplest, most jargon-free English possible, the "universal property of fold"?

I am working through "Real World Haskell", which led to to a free PDF called "A tutorial on the universality and expressiveness of fold". It makes the point that a "fold" is "universal". I am wrestling with his definition of "universal", and would…
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88
15
votes
1 answer

Is there a way to fold with index in Rust?

In Ruby, if I had an array a = [1, 2, 3, 4, 5] and I wanted to get the sum of each element times its index I could do a.each.with_index.inject(0) {|s,(i,j)| s + i*j} Is there an idiomatic way to do the same thing in Rust? So far, I…
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61