Anonymous function: Difference between revisions

Content deleted Content added
Rescuing 0 sources and tagging 1 as dead.) #IABot (v2.0.9.5
Tag: Reverted
Citation bot (talk | contribs)
Removed parameters. | Use this bot. Report bugs. | #UCB_CommandLine
Line 1:
{{short description|Function definition that is not bound to an identifier}}
In [[computer programming]], an '''anonymous function''' ('''function literal''', '''lambda abstraction''', '''lambda function''', '''lambda expression''' or '''block''') is a [[Function (computer science)|function]] definition that is not [[name binding|bound]] to an [[Name (computer science)|identifier]]. Anonymous functions are often arguments being passed to [[higher-order function]]s or used for constructing the result of a higher-order function that needs to return a function.<ref>{{cite web |title=Higher order functions |url=http://learnyouahaskell.com/higher-order-functions |url-status=live |access-date=3 December 2014 |website= |publisher=learnyouahaskell.com |language=en-US}}</ref>
If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in [[functional programming language]]s and other languages with [[first-class function]]s, where they fulfil the same role for the [[function type]] as [[literal (computer programming)|literals]] do for other [[data type]]s.
 
Line 8:
The names "lambda abstraction", "lambda function", and "lambda expression" refer to the notation of function abstraction in lambda calculus, where the usual function {{math|1={{itco|''f''}}(''x'') = ''M''}} would be written {{math|1=(λ''x''.{{itco|''M''}})}} ({{mvar|M}} is an expression that uses {{mvar|x}}). Compare to the Python syntax of <syntaxhighlight lang=python inline>lambda x: M</syntaxhighlight>.
 
The name "arrow function" refers to the mathematical "[[Maplet|maps to]]" symbol, {{math|''x'' ↦ ''M''}}. Compare to the JavaScript syntax of <syntaxhighlight lang=js inline>x => M</syntaxhighlight>.<ref>{{cite web |title=Arrow function expressions - JavaScript |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions |url-status=live |access-date=August 21, 2019 |website=MDN |language=en-US}}</ref>
 
==Uses==
Line 1,606:
* <code>FnOnce</code>: the closure captures by value (<code>T</code>). They are used for functions that are only called once.
 
With these traits, the compiler will capture variables in the least restrictive manner possible.<ref name="auto"/> They help govern how values are moved around between scopes, which is largely important since Rust follows a lifetime construct to ensure values are "borrowed" and moved in a predictable and explicit manner.<ref>{{Cite web |title=Lifetimes - Rust By Example |url=https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime.html |url-status=live |website=doc.rust-lang.org}}</ref>
 
The following demonstrates how one may pass a closure as an input parameter using the <code>Fn</code> trait: