User:Ushkin N/Comparison of programming languages/Functional/Fold
Appearance
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 |
ienum.Reverse() |
ienum |
ienum.Reverse() |
Aggregate is an extension method ienum is an IEnumerable<T> Similarly in all .NET languages | |
C++ | std::accumulate( |
std::accumulate( |
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}
|
{for {treeNode
|
{for {{treeNode.reverse}
|
also DefaultListModel and HashTable implement to-Iterator | |
D | reduce!func(initval, list) | reduce!func(initval, list |
reduce!func(list) | reduce!func( |
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))
|
All are extension methods on Java's Iterable interface, arrays are also supported | ||||
Groovy | list |
list.reverse() |
list |
list.reverse() |
||
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 |
stream.reduce |
||||
JavaScript 1.8 ECMAScript 5 |
array.reduce |
array.reduceRight |
array.reduce |
array.reduceRight |
||
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_reduce(array, func) | array_reduce( |
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 enum |
enum.reverse_each enum.reverse_each |
enum enum.reduce(&block) |
enum.reverse_each enum.reverse_each |
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() |
||||
Xtend | iterable.fold(initval,[func]) | iterable.reduce[func] |
References
[edit]- ^
For reference functools.reduce: import functools
For reference reduce: from functools import reduce - ^ Odersky, Martin (2008-01-05). "Re: Blog: My verdict on the Scala language". Newsgroup: comp.scala.lang. Retrieved 14 October 2013.