Content deleted Content added
fix code |
→In various languages: removed massive whitespace at the bottom of XPath 3; replaced valign attribute with vertical-align CSS |
||
Line 146:
{| class="wikitable" style="font-size: 85%"
! Language !! Left fold !! Right fold !! Left fold without initial value !! Right fold without initial value !! Unfold !! Notes
|-
| [[APL (programming language)|APL]]
| <code>''func''⍨/⌽''initval'',''vector''</code>
Line 154:
|
|
|-
| [[C Sharp (programming language)|C#]] [[C Sharp 3.0|3.0]]
| <code>''ienum''<wbr/>.Aggregate(''initval'', ''func'')</code>
Line 162:
|
| Aggregate is an [[extension method]]<br /> ''ienum'' is an <code>IEnumerable<T></code><br /> Similarly in all .NET languages
|-
| [[C++]]
| <code>std::accumulate(<wbr/>''begin'', ''end'', ''initval'', ''func'')</code>
Line 171:
| in header <code><numeric></code><br />''begin'', ''end'', ''rbegin'', ''rend'' are iterators<br />''func'' can be a [[function pointer]] or a [[function object]]
|
|-
| [[C++17]]
| <code>(''initval'' ''op'' ... ''op'' ''pack'')</code>
Line 180:
| Fold expression (only for variadic function templates): ''op'' is a binary operator (both ''op''s must be the same, e.g., <code>(std::cout << ... << args)</code>), ''pack'' is an unexpanded parameter pack.
|
|-
| [[CFML]]
| <code>obj.reduce(func,initial)</code>
Line 188:
|
| Where <code>func</code> receives as arguments the result of the previous operation (or the <code>initial</code> value on the first iteration); the current item; the current item's index or key; and a reference to the <code>obj</code>
|-
| [[Clojure]]
| <code>(reduce ''func'' ''initval'' ''list'')</code>
Line 196:
|
| See also [https://clojure.github.com/clojure/branch-master/clojure.core-api.html#clojure.core.reducers/fold clojure.core.reducers/fold]
|-
| [[Common Lisp]]
| <code>(reduce ''func'' ''list'' :initial-value ''initval'')</code>
Line 204:
|
|
|-
| [[Curl (programming language)|Curl]]
| <code>{{TreeNode.''default'' ''treeNode'' ...} ''.to-Iterator''}</code>
Line 212:
|
| also DefaultListModel and HashTable implement <code>to-Iterator</code>
|-
| [[D (programming language)|D]]
| <code>reduce!''func''(''initval'', ''list'')</code>
Line 220:
|
| in module <code>std.algorithm</code>
|-
| [[Elixir (programming language)|Elixir]]
| <code>List.foldl(list, acc, fun)</code>
Line 228:
|
| See [https://hexdocs.pm/elixir/List.html#foldl/3 documentation] for example usage
|-
| [[Elm (programming language)|Elm]]
| <code>List.foldl(''Fun'', ''Accumulator'', ''List'')</code>
Line 236:
|
| See also List API [http://package.elm-lang.org/packages/elm-lang/core/3.0.0/List#foldr]
|-
| [[Erlang (programming language)|Erlang]]
| <code>lists:foldl(''Fun'', ''Accumulator'', ''List'')</code>
Line 244:
|
|
|-
| [[F Sharp (programming language)|F#]]
| <code>Seq/List.fold ''func'' ''initval'' ''list''</code>
Line 252:
| <code>Seq.unfold ''func'' ''initval''</code>
|
|-
| [[Gosu (programming language)|Gosu]]
| <code>''Iterable''.fold(''f''(agg, e))<wbr/>''Iterable''.reduce(init, ''f''(agg, e)) <wbr/>''Iterable''.partition(''f''(e))</code>
Line 260:
|
| All are [[extension methods]] on Java's Iterable interface, arrays are also supported
|-
| [[Groovy (programming language)|Groovy]]
| <code>''list''<wbr/>.inject(''initval'', ''func'')</code>
Line 268:
|
|
|-
| [[Haskell (programming language)|Haskell]]
| <code>foldl ''func'' ''initval'' ''list''</code>
Line 276:
| <code>unfoldr ''func'' ''initval''</code>
| For foldl, the folding function takes arguments in the opposite order as that for foldr.
|-
| [[Haxe]]
| <code>Lambda.fold(''iterable'', ''func'', ''initval'')</code>
Line 284:
|
|
|-
| [[J (programming language)|J]]
| <code>''verb''<nowiki>~/|.</nowiki> ''initval'',''array''</code>
Line 292:
|
| u/y applies the dyad u between the items of y. [http://www.jsoftware.com/help/dictionary/d420.htm "J Dictionary: Insert"]
|-
| [[Java (programming language)|Java]] 8+
| <code>''stream''.reduce<wbr/>(''initval'', ''func'')</code>
Line 300:
|
|
|-
| [[JavaScript]] 1.8<br>[[ECMAScript]] 5
| <code>''array''.reduce<wbr/>(''func'', ''initval'')</code>
Line 308:
|
|
|-
| [[Julia (programming language)|Julia]]
| <code>foldl(''op'', ''itr''; [init])</code>
Line 324:
|
|Other collections also support <code>fold</code><ref>{{cite web |title=fold - Kotlin Programming Language |url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold.html |website=Kotlin |publisher=Jetbrains |access-date=29 March 2019 |ref=fold.kt}}</ref> and <code>reduce</code>.<ref>{{cite web |title=reduce - Kotlin Programming Language |url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/reduce.html |website=Kotlin |publisher=Jetbrains |access-date=29 March 2019 |ref=reduce.kt}}</ref> There is also <code>Result.fold(onSuccess, onFailure)</code>,<ref>{{cite web |title=Result - Kotlin Programming Language |url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.html |website=Kotlin |publisher=Jetbrains |access-date=29 March 2019 |ref=Result.kt}}</ref> which reduces a <code>Result<T></code> (either success or failure) to the return type of <code>onSuccess</code> and <code>onFailure</code>.
|-
| [[LFE (programming language)|LFE]]
| <code>(lists:foldl ''func'' ''accum'' ''list'')</code>
Line 332:
|
|
|-
| [[Logtalk]]
| <code>fold_left(Closure, Initial, List, Result)</code>
Line 340:
|
|Meta-predicates provided by the ''meta'' standard library object. The abbreviations ''foldl'' and ''foldr'' may also be used.
|-
| [[Maple (software)|Maple]]
| <code>foldl(''func'', ''initval'', ''sequence'')</code>
Line 348:
|
|
|-
| [[Mathematica]]
| <code>Fold[''func'', ''initval'', ''list'']</code>
Line 357:
| <code>Fold</code> without an initial value is supported in versions 10.0 and higher.
|
|-
| [[MATLAB]]
| <code>fold(@''func'', ''list'', ''defaultVal'')</code>
Line 366:
| Requires Symbolic Math Toolbox, supported from R2016b.
|
|-
| [[Maxima (software)|Maxima]]
| <code>lreduce(''func'', ''list'', ''initval'')</code>
Line 374:
|
|
|-
| [[Mythryl]]
| <code>fold_left ''func'' ''initval'' ''list''<br /> vector::fold_left ''func'' ''initval'' ''vector''</code>
Line 382:
|
| The supplied function takes its arguments in a tuple.
|-
| [[OCaml]]
| <code>List.fold_left ''func'' ''initval'' ''list''<br /> Array.fold_left ''func'' ''initval'' ''array''</code>
Line 390:
| <code>Base.Sequence.unfold ''~init'' ''~f''</code> <ref>{{cite web|url=https://opensource.janestreet.com/base/|title = Base|publisher = Jane Street Capital|access-date = February 26, 2019}}</ref>
|
|-
| [[Oz (programming language)|Oz]]
| <code>{FoldL ''List'' ''Func'' ''InitVal''}</code>
Line 398:
|
|
|-
| [[PARI/GP]]
| <code>fold( ''f'', ''A'' )</code>
Line 406:
|
|
|-
| [[Perl]]
| <code>reduce ''block'' ''initval'', ''list''</code>
Line 414:
|
| in <code>List::Util</code> module
|-
| [[PHP]]
| <code>array_reduce(''array'', ''func'', ''initval'')</code>
Line 422:
|
| When ''initval'' is not supplied, NULL is used, so this is not a true foldl1. Before PHP 5.3, ''initval'' can only be integer. "func" is a [http://php.net/manual/en/language.pseudo-types.php#language.types.callback callback]. Try [http://micmap.org/php-by-example/en/function/array_reduce array_reduce] online.
|-
| [[Python (programming language)|Python]] 2.x
| <code>reduce(''func'', ''list'', ''initval'')</code>
Line 430:
|
|
|-
| [[Python (programming language)|Python]] 3.x
| <code>functools.reduce(''func'', ''list'', ''initval'')</code>
Line 440:
For reference <code>''functools.reduce''</code>: <code>import functools</code><BR />
For reference <code>''reduce''</code>: <code>from functools import reduce</code></ref>
|-
| [[R (programming language)|R]]
| <code>Reduce(''func'', ''list'', ''initval'')</code>
Line 448:
|
| 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 (programming language)|Ruby]]
| <code>''enum''<wbr/>.inject(''initval'', ''&block'')<br /> ''enum''<wbr/>.reduce(''initval'', ''&block'')</code>
Line 456:
|
| In Ruby 1.8.7+, can also pass a symbol representing a function instead of a block. <br /> ''enum'' is an Enumeration <br /> Please notice that these implementations of right folds are wrong for non-commutative ''&block'' (also initial value is put on wrong side).
|-
| [[Rust (programming language)|Rust]]
| <code>''iterator''.fold(''initval'', ''func'')</code>
Line 464:
|
|
|-
| [[Scala (programming language)|Scala]]
| <code>''list''.foldLeft(''initval'')(''func'')</code><br /><code>(''initval'' /: ''list'')(''func'')</code>
Line 473:
| Scala's symbolic fold syntax was intended to resemble the left- or right-leaning tree commonly used to explain the fold operation,<ref>{{cite newsgroup |title= Re: Blog: My verdict on the Scala language |author= Odersky, Martin |date= 2008-01-05 |newsgroup= comp.scala.lang |url= http://permalink.gmane.org/gmane.comp.lang.scala/9557 |access-date= 14 October 2013}}</ref> but has since been reinterpreted as an illustration of a toppling domino.<ref>{{cite web|last1=Sterling|first1=Nicholas|title=An intuitive feel for Scala’s /: operator (foldLeft)|url=https://nicholassterling.wordpress.com/2010/07/28/an-intuition-about-scalas-operator-foldleft/|access-date=24 June 2016}}</ref> The colon comes from a general Scala syntax mechanism whereby the apparent infix operator is invoked as a method on the left operand with the right operand passed as an argument, or vice versa if the operator's last character is a colon, here applied symmetrically.
Scala also features the tree-like folds using the method <code>list.fold(z)(op)</code>.<ref>{{Cite web|url=https://www.scala-lang.org/api/current/scala/collection/Seq.html#fold%5BA1%3E:A%5D(z:A1)(op:(A1,A1)=%3EA1):A1|title=Fold API - Scala Standard Library|website=www.scala-lang.org|access-date=2018-04-10}}</ref>
|-
| [[Scheme (programming language)|Scheme]] R<sup>6</sup>RS
| <code>(fold-left ''func'' ''initval'' ''list'')<br /> (vector-fold ''func'' ''initval'' ''vector'')</code>
Line 481:
|
| srfi/1 srfi/43
|-
| [[Smalltalk]]
| <code>''aCollection'' inject: ''aValue'' into: ''aBlock''</code>
Line 489:
|
| ANSI Smalltalk doesn't define #reduce: but many implementations do.
|-
| [[Standard ML]]
| <code>foldl ''func'' ''initval'' ''list''<br /> Array.foldl ''func'' ''initval'' ''array''</code>
Line 497:
|
| The supplied function takes its arguments in a tuple. For foldl, the folding function takes arguments in the same order as for foldr.
|-
| [[Swift (programming language)|Swift]]
| <code>''array''.reduce(''initval'', ''func'')<br /> reduce(''sequence'', ''initval'', ''func'')</code>
Line 505:
|
|
|- style="vertical-align: top;"
|[[XPath 3|XPath 3.1]]
|<code>[https://www.w3.org/TR/xpath-functions-31/#func-array-fold-left ''array'':fold-left](</code>
Line 539:
<code>) as item()*</code>
|<code>[https://www.w3.org/TR/xpath-functions-31/#func-array-fold-right ''array'':fold-right](</code>
Line 581 ⟶ 571:
<code>) as item()*</code>
|
|
Line 598 ⟶ 578:
The difference in the signatures is due to the fact that the value of an <code>array</code> item can be a <code>sequence</code>, while XPath doesn't have <code>sequence</code> of <code>sequence</code>s
|- style="vertical-align: top;"
| [[Xtend]]
| <code>''iterable''.fold(''initval'',[''func''])</code>
|