User:Ushkin N/Comparison of programming languages/Functional/Fold

See Fold (higher-order function).

Language Left fold Right fold Left fold without initial value Right fold without initial value Unfold Notes
APL function⍨/⌽initval,vector function/vector,initval function⍨/⌽vector function/vector
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<T>
Similarly in all .NET languages
C++ std::accumulate(begin, end, initval, func) std::accumulate(rbegin, rend, initval, func) in header <numeric>
begin, end, rbegin, rend are iterators
func can be a function pointer or a function object
CFML obj.reduce(func,initial) obj.reduce(func) Where func receives as arguments the result of the previous operation (or the initial value on the first iteration); the current item; the current item's index or key; and a reference to the obj
Clojure (reduce func initval list) (reduce func initval (reverse list')) (reduce func list) (reduce func" (reverse list)) See also clojure.core.reducers/fold
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
Elm List.foldl(Fun, Accumulator, List) List.foldr(Fun, Accumulator, List) See also List API [1]
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 Seq.unfold func initval
Gosu Iterable.fold(f(agg, e))Iterable.reduce(init, f(agg, e)) Iterable.partition(f(e))

All are extension methods on Java's Iterable interface, arrays are also supported
Groovy list.inject(initval, func) list.reverse().inject(initval, func) list.inject(func) list.reverse().inject(func)
Haskell foldl func initval list foldr func initval list foldl1 func list foldr1 func list unfoldr func initval
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"
Java 8+ stream.reduce(initval, func) stream.reduce(func)
JavaScript 1.8
ECMAScript 5
array.reduce(func, initval) array.reduceRight(func, initval) array.reduce(func) array.reduceRight(func)
LFE (lists:foldl func accum list) (lists:foldr func accum list)
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[func, initval, list] Fold[func, initval, Reverse[list]] Fold[func, list] Fold[func, Reverse[list]] NestWhileList[func,, initval, predicate] Fold without an initial value is supported in versions 10.0 and higher.
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_reverse(array), func, initval) array_reduce(array, func) array_reduce(array_reverse(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. Try array_reduce online.
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.[1]
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(&block)
In Ruby 1.8.7+, can also pass a symbol representing a function instead of a block.
enum is an Enumeration
Please notice that these implementations of right folds are wrong for non-commutative &block (also initial value is put on wrong side).
Scala list.foldLeft(initval)(func)
(initval /: list)(func)
list.foldRight(initval)(func)
(list :\ initval){func}
list.reduceLeft(func) list.reduceRight(func) Scala's symbolic fold syntax is intended to resemble the left or right-leaning tree commonly used to explain the fold operation.[2]
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.
Swift array.reduce(initval, func)
reduce(sequence, initval, func)
array.reverse().reduce(initval, func)
Xtend iterable.fold(initval,[func]) iterable.reduce[func]

References

edit
  1. ^ For reference functools.reduce: import functools
    For reference reduce: from functools import reduce
  2. ^ Odersky, Martin (2008-01-05). "Re: Blog: My verdict on the Scala language". Newsgroupcomp.scala.lang. Retrieved 14 October 2013.