7/21/08

Scala: fold/reduce cheatsheet

My fold/reduce 'cheat sheet' is something like:

For the list (a,b,c,d) fold/reduce is:

reduceLeft(f) => f(f(f(a,b),c),d)

foldLeft(Z)(f) => f(f(f(f(Z,a),b),c),d)

("applies the function from the left")


reduceRight(f) => f(a,f(b,f(c,d)))

foldRight(Z)(f) => f(a,f(b,f(c,f(d,Z))))

("
applies the function from the right")

[EDIT: as you see, fold and reduce are essentially the same, but fold starts with an outside element ]
Post a Comment