Content deleted Content added
m Reverted edits by 58.69.114.92 (talk) (HG) (3.4.13) |
m Improved clarity and tone in descriptions of anonymous functions, adjusted technical phrasing, and fixed formatting in examples and templates. |
||
(One intermediate revision by one other user not shown) | |||
Line 3:
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. 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 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 128:
</syntaxhighlight>
The anonymous function
<syntaxhighlight lang="python">
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}}
Line 497:
|}
== Examples of anonymous functions ==
{{main|Examples of anonymous functions}}
|