Content deleted Content added
Updated short description Tags: Reverted Mobile edit Mobile app edit Android app edit App description change |
m Improved clarity and tone in descriptions of anonymous functions, adjusted technical phrasing, and fixed formatting in examples and templates. |
||
(12 intermediate revisions by 11 users not shown) | |||
Line 1:
{{short description|
In [[computer programming]], an '''anonymous function''' ('''function literal''', '''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 |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.
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 {{mvar|M}} is an expression that uses {{mvar|x}}. Compare to the Python syntax of <syntaxhighlight lang=python inline>lambda x: M</syntaxhighlight>.
Line 13 ⟶ 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. Anonymous functions often provide a briefer notation than defining named functions. In languages that do not permit the definition of named functions in local scopes, anonymous functions may provide encapsulation via localized scope, however the code in the body of such anonymous function may not be re-usable, or amenable to separate testing. Short/simple anonymous functions used in expressions may be easier to read and understand than separately defined named functions, though
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 named function.
Line 67 ⟶ 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 92 ⟶ 91:
{{Main|currying}}
Currying
<syntaxhighlight lang="python">
>>> def divide(x, y):
Line 116 ⟶ 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 125 ⟶ 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 164 ⟶ 163:
>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5]
>>> reduce(lambda x, y: x * y, a)
120
</syntaxhighlight>
Line 186 ⟶ 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}}
Line 230 ⟶ 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 331 ⟶ 330:
| [[Java (programming language)|Java]]
| {{yes|{{Y}}}}
| Supported
|-
| [[JavaScript]]
Line 439 ⟶ 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 463 ⟶ 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]]
Line 498 ⟶ 497:
|}
== Examples of anonymous functions ==
{{main|Examples of anonymous functions}}
==See also==
{{Portal|Computer programming}}
* [[First-class function]]
* [[Lambda calculus definition]]
==References==
Line 1,774 ⟶ 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}}
|