Content deleted Content added
m Improved clarity and tone in descriptions of anonymous functions, adjusted technical phrasing, and fixed formatting in examples and templates. |
|||
(45 intermediate revisions by 35 users not shown) | |||
Line 1:
{{short description|Function definition that is not bound to an identifier}}
In [[computer programming]], an '''anonymous function''' ('''function literal''', '''
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.
Anonymous functions originate in the work of [[Alonzo Church]] in his invention of the [[lambda calculus]], in which all functions are anonymous, in 1936, before electronic computers.<ref>{{citation|title=Models of Computation: An Introduction to Computability Theory|series=Undergraduate Topics in Computer Science|first=Maribel|last=Fernandez|publisher=Springer Science & Business Media|year=2009|isbn=9781848824348|page=33|url=https://books.google.com/books?id=FPFsnzzebhQC&pg=PA33|quote=The Lambda calculus ... was introduced by Alonzo Church in the 1930s as a precise notation for a theory of anonymous functions}}</ref> In several programming languages, anonymous functions are introduced using the keyword ''lambda'', and anonymous functions are often referred to as '''lambdas''' or '''lambda abstractions'''. Anonymous functions have been a feature of [[programming language]]s since [[Lisp (programming language)|Lisp]] in 1958, and a growing number of modern programming languages support anonymous functions.
{{toclimit|3}}
== Names ==
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''}})}}, and where
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
==Uses==
{{Unreferenced Section|date=February 2018}}
Anonymous functions can
The use of anonymous functions is a matter of style. Using them is never the only way to solve a problem; each anonymous function could instead be defined as a named function and called by name.
In some programming languages, anonymous functions are commonly implemented for very specific purposes such as binding events to callbacks or instantiating the function for particular values, which may be more efficient in a [[Dynamic programming language]], more readable, and less error-prone than calling a
The following examples are written in Python 3.
Line 66:
{{Main|Closure (computer programming)}}
Closures are functions evaluated in an environment containing [[bound variable]]s. The following example binds the variable "threshold"
<syntaxhighlight lang="python">
Line 91:
{{Main|currying}}
Currying
<syntaxhighlight lang="python">
>>> def divide(x, y):
Line 115:
===Higher-order functions===
A [[higher-order function]] is a function that takes a function as an argument or returns one as a result. This technique is
====Map====
Line 124:
<syntaxhighlight lang="python">
>>> a = [1, 2, 3, 4, 5, 6]
>>> list(map(lambda x: x * x, a))
[1, 4, 9, 16, 25, 36]
</syntaxhighlight>
The anonymous function
<syntaxhighlight lang="python">
>>> a = [1, 2, 3, 4, 5, 6]
>>> [x * x for x in a]
[1, 4, 9, 16, 25, 36]
</syntaxhighlight>
Line 163:
>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5]
>>> reduce(lambda x, y: x * y, a)
120
</syntaxhighlight>
Line 185:
The anonymous function here is the multiplication of the two arguments.
==List of languages==
The following is a list of [[programming language]]s that support unnamed anonymous functions fully, or partly as some variant, or not at all.
{{Expand list|date=August 2008}}
{{sticky header}}
{| class="sortable wikitable sticky-header" style="text-align: left; font-size: 0.92em;"
|+ List of languages
|-
! Language !! Support !! Notes
|-
Line 203 ⟶ 204:
|-
| [[Ada (programming language)|Ada]]
| {{
| Expression functions are a part of Ada2012, access-to-subprogram<ref>{{Cite web |title=Access Types |url=https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-10.html#S0082 |access-date=2024-06-27 |website=www.adaic.org}}</ref>
|-
| [[ALGOL 68]]
Line 228 ⟶ 229:
| [[C (programming language)|C]]
| {{no|{{N}}}}
| Support is provided in [[Clang]] and along with the [[LLVM]] compiler-rt lib. GCC support is given for a macro implementation which enables the possibility of use. See [[#Examples of anonymous functions|below]] for more details.
|-
| [[C Sharp (programming language)|C#]]
Line 248 ⟶ 249:
| [[COBOL]]
| {{no|{{N}}}}
| [[Micro Focus]]'s non-standard Managed COBOL dialect supports lambdas, which are called anonymous delegates/methods.<ref>{{cite web | url=http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs/GUID-DA75663F-6357-4064-8112-C87E7457DE51.html | title=Managed COBOL Reference | publisher=[[Micro Focus]] | work=Micro Focus Documentation | access-date=25 February 2014 | archive-url=https://archive.today/20140225190401/http://documentation.microfocus.com/help/topic/com.microfocus.eclipse.infocenter.visualcobol.vs/GUID-DA75663F-6357-4064-8112-C87E7457DE51.html | archive-date=25 February 2014}}</ref>
|-
| [[Curl (programming language)|Curl]]
Line 329 ⟶ 330:
| [[Java (programming language)|Java]]
| {{yes|{{Y}}}}
| Supported since [[Java 8]].
|-
| [[JavaScript]]
Line 437 ⟶ 438:
| [[Ruby (programming language)|Ruby]]
| {{yes|{{Y}}}}
| Ruby's anonymous functions, inherited from [[Smalltalk]], are called [[Ruby (programming language)#Blocks and iterators|blocks]].<ref name=":10">{{cite web|url=http://www.reactive.io/tips/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ |title=Understanding Ruby Blocks, Procs and Lambdas |last=Sosinski |first=Robert |publisher=Reactive.IO |date=2008-12-21 |access-date=2014-05-30 |url-status=dead |archive-url=https://web.archive.org/web/20140531123646/http://www.reactive.io/tips/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ |archive-date=2014-05-31 }}</ref>
|-
| [[Rust (programming language)|Rust]]
Line 461 ⟶ 462:
| [[Swift (programming language)|Swift]]
| {{yes|{{Y}}}}
| Swift's anonymous functions are called Closures.<ref name=":9">{{Cite web|url=https://docs.swift.org/swift-book/LanguageGuide/Closures.html|title=Closures — The Swift Programming Language (Swift 5.5)|website=docs.swift.org}}</ref>
|-
| [[TypeScript]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Documentation - Everyday Types|url=https://www.typescriptlang.org/docs/handbook/2/everyday-types.html|access-date=2022-01-14|website=www.typescriptlang.org|language=en}}</ref>
|-
| [[Typst]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Function Type - Typst Documentation|url=https://typst.app/docs/reference/foundations/function/#unnamed|access-date=2024-09-10|website=typst.app|language=en}}</ref>
|-
| [[Tcl]]
Line 486 ⟶ 491:
| {{yes|{{Y}}}}
|<ref name=":8">{{Cite web|title=Pure Anonymous Function: Elementary Introduction to the Wolfram Language|url=https://www.wolfram.com/language/elementary-introduction/2nd-ed/26-pure-anonymous-functions.html.en|access-date=2022-01-14|website=www.wolfram.com|language=en}}</ref>
|-
| [[Zig (programming language)| Zig]]
| {{no|{{N}}}}
| <ref>{{Cite web |title=Lambdas, Closures and everything in between · Issue #1048 · ziglang/zig |url=https://github.com/ziglang/zig/issues/1048 |access-date=2023-08-21 |website=GitHub |language=en}}</ref>
|}
== Examples of anonymous functions ==
{{main|Examples of anonymous functions}}
==See also==
{{Portal|Computer programming}}
* [[First-class function]]
* [[Lambda calculus definition]]
==References==
Line 1,754 ⟶ 514:
* [https://web.archive.org/web/20160308090330/http://webwidetutor.com/php/php-anonymous-functions-?id=12 php anonymous functions] php anonymous functions
* [http://dobegin.com/lambda-functions-everywhere/ Lambda functions in various programming languages]
* {{usurped|1=[https://web.archive.org/web/20230314153037/https://learngolangonline.com/functions Functions in Go]}}
{{Authority control}}
Line 1,763 ⟶ 524:
[[Category:Subroutines]]
[[Category:Articles with example code]]
[[Category:Articles with example C code]]
[[Category:Articles with example C++ code]]
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example D code]]
[[Category:Articles with example Java code]]
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example Julia code]]
[[Category:Articles with example Lisp (programming language) code]]
[[Category:Articles with example MATLAB/Octave code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example R code]]
[[Category:Articles with example Ruby code]]
[[Category:Articles with example Scala code]]
[[Category:Articles with example Smalltalk code]]
[[Category:Articles with example Tcl code]]
|