Content deleted Content added
m Revise Ada support, citate |
m Improved clarity and tone in descriptions of anonymous functions, adjusted technical phrasing, and fixed formatting in examples and templates. |
||
(18 intermediate revisions by 17 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 ==
Line 12:
==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 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 329 ⟶ 330:
| [[Java (programming language)|Java]]
| {{yes|{{Y}}}}
| Supported
|-
| [[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 492 ⟶ 497:
|}
== Examples of anonymous functions ==
{{main|Examples of anonymous functions}}
==See also==
{{Portal|Computer programming}}
* [[First-class function]]
* [[Lambda calculus definition]]
==References==
Line 1,768 ⟶ 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}}
|