Fold (higher-order function): Difference between revisions

Content deleted Content added
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 9 templates: del empty params (1×);
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 9 templates: hyphenate params (8×);
Line 324:
|''<code>Iterable</code>''<code>.reduceRight''(func'')</code>
|
|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 |accessdateaccess-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 |accessdateaccess-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 |accessdateaccess-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>.
|- valign="top"
| [[LFE (programming language)|LFE]]
Line 389:
|
|
| <code>Base.Sequence.unfold ''~init'' ''~f''</code> <ref>{{cite web|url=https://opensource.janestreet.com/base/|title = Base|publisher = Jane Street Capital|accessdateaccess-date = February 26, 2019}}</ref>
|
|- valign="top"
Line 472:
| <code>''list''.reduceRight(''func'')</code>
|
| 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 |accessdateaccess-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/|accessdateaccess-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>
|- valign="top"
Line 637:
|pages= 355–372
|url= http://www.cs.nott.ac.uk/~gmh/fold.pdf
|accessdateaccess-date= March 26, 2009}}</ref>
<syntaxhighlight lang="haskell">
g = foldr f v
Line 650:
|pages= 5–16
|url= http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf
|accessdateaccess-date= May 1, 2011}}</ref> proving that iterations can be reduced to folds:
<syntaxhighlight lang="haskell"> y f = foldr (\_ -> f) undefined (repeat undefined)</syntaxhighlight>