Fold (higher-order Function) - Folds in Various Languages

Folds in Various Languages

Language Left fold Right fold Left fold without initial value Right fold without initial value Notes
C# 3.0 ienum.Aggregate(initval, func) ienum.Reverse.Aggregate(initval, func) ienum.Aggregate(func) ienum.Reverse.Aggregate(func) Aggregate is an extension method
ienum is an IEnumerable
Similarly in all .NET languages
C++ std::accumulate(begin, end, initval, func) std::accumulate(rbegin, rend, initval, func) in header
begin, end, rbegin, rend are iterators
func can be a function pointer or a function object
Clojure (reduce func initval list) (reduce func list)
Common Lisp (reduce func list :initial-value initval) (reduce func list :from-end t :initial-value initval) (reduce func list) (reduce func list :from-end t)
Curl {{TreeNode.default treeNode ...} .to-Iterator} {{TreeNode.default treeNode ...} .reverse}.to-Iterator} {for {treeNode.to-Iterator} do} {for {{treeNode.reverse}.to-Iterator} do} also DefaultListModel and HashTable implement to-Iterator
D reduce!func(initval, list) reduce!func(initval, list.reverse) reduce!func(list) reduce!func(list.reverse) in module std.algorithm
Erlang lists:foldl(Fun, Accumulator, List) lists:foldr(Fun, Accumulator, List)
F# Seq/List.fold func initval list List.foldBack func list initval Seq/List.reduce func list List.reduceBack func list
Groovy list.inject(initval, func) list.reverse.inject(initval, func) list.inject(list, func) list.inject(list, func)
Haskell foldl func initval list foldr func initval list foldl1 func list foldr1 func list
haXe Lambda.fold(iterable, func, initval)
J verb~/|. initval,array verb/ array,initval verb~/|. array verb/ array u/y applies the dyad u between the items of y. "J Dictionary: Insert"
JavaScript 1.8 array.reduce(func, initval) array.reduceRight(func, initval) array.reduce(func) array.reduceRight(func)
Logtalk fold_left(Closure, Initial, List, Result) fold_right(Closure, Initial, List, Result) Meta-predicates provided by the meta standard library object. The abbreviations foldl and foldr may also be used.
Maple foldl(func, initval, sequence) foldr(func, initval, sequence)
Mathematica Fold
Maxima lreduce(func, list, initval) rreduce(func, list, initval) lreduce(func, list) rreduce(func, list)
Mythryl fold_left func initval list
vector::fold_left func initval vector
fold_right func initval list
vector::fold_right func initval vector
The supplied function takes its arguments in a tuple.
OCaml List.fold_left func initval list
Array.fold_left func initval array
List.fold_right func list initval
Array.fold_right func array initval
Oz {FoldL List Func InitVal} {FoldR List Func InitVal}
Perl reduce block initval, list reduce block list in List::Util module
PHP array_reduce(array, func, initval) array_reduce(array, func) When initval is not supplied, NULL is used, so this is not a true foldl1. Prior to PHP 5.3, initval can only be integer. "func" is a callback.
Python 2.x reduce(func, list, initval) reduce(lambda x,y: func(y,x), reversed(list), initval) reduce(func, list) reduce(lambda x,y: func(y,x), reversed(list))
Python 3.x functools.reduce(func, list, initval) functools.reduce(lambda x,y: func(y,x), reversed(list), initval) functools.reduce(func, list) functools.reduce(lambda x,y: func(y,x), reversed(list)) In module functools.

For reference functools.reduce: import functools
For reference reduce: from functools import reduce

R Reduce(func, list, initval) Reduce(func, list, initval, right=TRUE) Reduce(func, list) Reduce(func, list, right=TRUE) R supports right folding and left or right folding with or without an initial value through the right and init arguments to the Reduce function.
Ruby enum.inject(initval, &block)
enum.reduce(initval, &block)
enum.reverse_each.inject(initval, &block)
enum.reverse_each.reduce(initval, &block)
enum.inject(&block)
enum.reduce(&block)
enum.reverse_each.inject(&block)
enum.reverse_each.reduce(initval, &block)
In Ruby 1.8.7+, can also pass a symbol representing a function instead of a block.
enum is an Enumeration
If enum is array use reverse instead of reverse_each
Scala list.foldLeft(initval)(func) list.foldRight(initval)(func) list.reduceLeft(func) list.reduceRight(func)
Scheme R6RS (fold-left func initval list)
(vector-fold func initval vector)
(fold-right func initval list)
(vector-fold-right func initval vector)
(reduce-left func defaultval list) (reduce-right func defaultval list) srfi/1 srfi/43
Smalltalk aCollection inject: aValue into: aBlock aCollection reduce: aBlock ANSI Smalltalk doesn't define #reduce: but many implementations do.
Standard ML foldl func initval list
Array.foldl func initval array
foldr func initval list
Array.foldr func initval array
The supplied function takes its arguments in a tuple. For foldl, the folding function takes arguments in the reverse of the traditional order.

Read more about this topic:  Fold (higher-order Function)

Famous quotes containing the words folds and/or languages:

    Bunched upside down, they sleep in air.
    Their sharp ears, their sharp teeth, their quick sharp faces
    Are dull and slow and mild.
    All the bright day, as the mother sleeps,
    She folds her wings about her sleeping child.
    Randall Jarrell (1914–1965)

    No doubt, to a man of sense, travel offers advantages. As many languages as he has, as many friends, as many arts and trades, so many times is he a man. A foreign country is a point of comparison, wherefrom to judge his own.
    Ralph Waldo Emerson (1803–1882)