Anonymous function: Difference between revisions

Content deleted Content added
m Reverted edits by 58.69.114.92 (talk) (HG) (3.4.13)
HelloHop (talk | contribs)
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 be used for containingencapsulate functionality that needdoes not berequire namednaming and possiblyis intended for short-term or localized use. Some notable examples include [[Closure (computer science)|closures]] and [[currying]].
 
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 withoutlacking a [[Naming_convention_Naming convention (programming)|descriptive name]] they may be more difficultreduce tocode understandreadability.
 
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" inwithin an anonymous function that compares the input values to thethis threshold.
 
<syntaxhighlight lang="python">
Line 91:
{{Main|currying}}
 
Currying is the process of changingtransforms a function so that rather than takingtakes multiple inputs,arguments it takesinto a singlesequence inputof andfunctions returnseach accepting a function which accepts the second input, and sosingle forthargument. In this example, a function that performs [[integer division|division]] by any integer is transformed into one that performs division by a set integer.
<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 commonlyfrequently usedemployed to customizetailor the behavior of a generically defined function, oftensuch aas loopinga constructloop or recursion schemepattern. Anonymous functions are a convenient way to specify such function arguments. The following examples are in Python 3.
 
====Map====
Line 128:
</syntaxhighlight>
 
The anonymous function acceptstakes an argument and multipliesreturns itits by itself (squares it)square. The above form is discouraged by the creators of the language, who maintain that the form presented below has the same meaning and is more aligned with the philosophy of the language:
 
<syntaxhighlight lang="python">
Line 185:
The anonymous function here is the multiplication of the two arguments.
 
TheA resultfold ofdoes anot foldnecessarily needproduce nota besingle onescalar value; it can also generate structured results such as lists. Instead, both map and filter can be created using fold. In map, the value that is accumulated is a new list, containing the results of applying a function to each element of the original list. In filter, the value that is accumulated is a new list containing only those elements that match the given condition.
 
==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.
 
ThisThe following table showsillustrates someseveral generalcommon trendspatterns. FirstNotably, the languages thatlike do not support anonymous functions ([[C (programming language)|C]], [[Pascal (programming language)|Pascal]], and [[Object Pascal]])—which traditionally do not support anonymous arefunctions—are all [[statically typed]] languages. However, statically typed languages can support anonymous functions. For example, the [[ML (programming language)|ML]] languages are statically typed and fundamentally include anonymous functions, and [[Delphi (programming language)|Delphi]], a dialect of [[Object Pascal]], has been extended to support anonymous functions, as has [[C++]] (by the [[C++11]] standard). Second, the languages that treat functions as [[first-class function]]s ([[Dylan (programming language)|Dylan]], [[Haskell (programming language)|Haskell]], [[JavaScript]], [[Lisp (programming language)|Lisp]], [[ML (programming language)|ML]], [[Perl]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[Scheme (programming language)|Scheme]]) generally have anonymous function support so that functions can be defined and passed around as easily as other data types.
 
{{Expand list|date=August 2008}}
Line 497:
|}
 
== Examples of anonymous functions ==
 
{{main|Examples of anonymous functions}}