Content deleted Content added
→Examples: Link languages |
m Improved clarity and tone in descriptions of anonymous functions, adjusted technical phrasing, and fixed formatting in examples and templates. |
||
(954 intermediate revisions by more than 100 users not shown) | |||
Line 1:
{{short description|Function definition that is not bound to an identifier}}
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>.
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 |access-date=August 21, 2019 |website=MDN |language=en-US}}</ref>
==
{{Unreferenced Section|date=February 2018}}
Anonymous functions can encapsulate functionality that does not require naming and is 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 lacking a [[Naming convention (programming)|descriptive name]] may reduce code readability.
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.
The following examples are written in Python 3.
===
When attempting to sort in a non-standard way, it may be easier to contain the sorting logic as an anonymous function instead of creating a named function.
Most languages provide a generic sort function that implements a [[sort algorithm]] that will sort arbitrary objects.
This function usually accepts an arbitrary function that determines how to compare whether two elements are equal or if one is greater or less than the other.
Consider this Python code sorting a list of strings by length of the string:
<syntaxhighlight lang="python">
>>> a = ['house', 'car', 'bike']
>>> a.sort(key=lambda x: len(x))
>>> a
['car', 'bike', 'house']
</syntaxhighlight>
The anonymous function in this example is the lambda expression:
<syntaxhighlight lang="python">
lambda x: len(x)
</syntaxhighlight>
The anonymous function accepts one argument, <code>x</code>, and returns the length of its argument, which is then used by the <code>sort()</code> method as the criteria for sorting.
Basic syntax of a lambda function in Python is
<syntaxhighlight lang="python">
lambda arg1, arg2, arg3, ...: <operation on the arguments returning a value>
</syntaxhighlight>
The expression returned by the lambda function can be assigned to a variable and used in the code at multiple places.
<syntaxhighlight lang="python">
>>> add = lambda a: a + a
>>> add(20)
40
</syntaxhighlight>
Another example would be sorting items in a list by the name of their class (in Python, everything has a class):
<syntaxhighlight lang="python">
>>> a = [10, 'number', 11.2]
>>> a.sort(key=lambda x: x.__class__.__name__)
>>> a
[11.2, 10, 'number']
</syntaxhighlight>
Note that <code>11.2</code> has class name "<code>float</code>", <code>10</code> has class name "<code>int</code>", and <code>'number'</code> has class name "<code>str</code>". The sorted order is "<code>float</code>", "<code>int</code>", then "<code>str</code>".
===Closures===
{{Main|Closure (computer programming)}}
Closures are functions evaluated in an environment containing [[bound variable]]s. The following example binds the variable "threshold" within an anonymous function that compares input values to this threshold.
<syntaxhighlight lang="python">
def comp(threshold):
return lambda x: x < threshold
</syntaxhighlight>
This can be used as a sort of generator of comparison functions:
<syntaxhighlight lang="python">
>>> func_a = comp(10)
>>> func_b = comp(20)
>>> print(func_a(5), func_a(8), func_a(13), func_a(21))
True True False False
>>> print(func_b(5), func_b(8), func_b(13), func_b(21))
True True True False
</syntaxhighlight>
It would be impractical to create a function for every possible comparison function and may be too inconvenient to keep the threshold around for further use. Regardless of the reason why a closure is used, the anonymous function is the entity that contains the functionality that does the comparing.
===Currying===
{{Main|currying}}
Currying transforms a function that takes multiple arguments into a sequence of functions each accepting a single argument. 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):
... return x / y
>>> def divisor(d):
... return lambda x: divide(x, d)
>>> half = divisor(2)
>>> third = divisor(3)
>>> print(half(32), third(32))
16.0 10.666666666666666
>>> print(half(40), third(40))
20.0 13.333333333333334
</syntaxhighlight>
While the use of anonymous functions is perhaps not common with currying, it still can be used. In the above example, the function divisor generates functions with a specified divisor. The functions half and third curry the divide function with a fixed divisor.
The divisor function also forms a closure by binding the variable <code>d</code>.
===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 frequently employed to tailor the behavior of a generically defined function, such as a loop or recursion pattern. Anonymous functions are a convenient way to specify such function arguments. The following examples are in Python 3.
====Map====
{{Main|Map (higher-order function)}}
The map function performs a function call on each element of a list. The following example [[Square (algebra)|square]]s every element in an array with an anonymous function.
<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 takes an argument and returns its 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">
>>> a = [1, 2, 3, 4, 5, 6]
>>> [x * x for x in a]
[1, 4, 9, 16, 25, 36]
</syntaxhighlight>
====Filter====
{{Main|Filter (higher-order function)}}
The filter function returns all elements from a list that evaluate True when passed to a certain function.
<syntaxhighlight lang="python">
>>> a = [1, 2, 3, 4, 5, 6]
>>> list(filter(lambda x: x % 2 == 0, a))
[2, 4, 6]
</syntaxhighlight>
The anonymous function checks if the argument passed to it is even. The same as with map, the form below is considered more appropriate:
<syntaxhighlight lang="python">
>>> a = [1, 2, 3, 4, 5, 6]
>>> [x for x in a if x % 2 == 0]
[2, 4, 6]
</syntaxhighlight>
====Fold====
{{Main|Fold (higher-order function)}}
A fold function runs over all elements in a structure (for lists usually left-to-right, a "left fold", called <code>reduce</code> in Python), accumulating a value as it goes. This can be used to combine all elements of a structure into one value, for example:
<syntaxhighlight lang="python">
>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5]
>>> reduce(lambda x, y: x * y, a)
120
</syntaxhighlight>
This performs
:<math>
\left(
\left(
\left(
1 \times 2
\right)
\times 3
\right)
\times 4
\right)
\times 5
= 120.
</math>
The anonymous function here is the multiplication of the two arguments.
A fold does not necessarily produce a single scalar 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.
The following table illustrates several common patterns. Notably, languages like [[C (programming language)|C]], [[Pascal (programming language)|Pascal]], and [[Object Pascal]]—which traditionally do not support anonymous functions—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}}
{{sticky header}}
{| class="sortable wikitable sticky-header" style="text-align: left; font-size: 0.92em;"
|+ List of languages
|-
! Language !! Support !! Notes
|-
| [[ActionScript]]
| {{yes|{{Y}}}}
|
|-
| [[Ada (programming language)|Ada]]
| {{partial|{{Y}}}}
| 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]]
| {{yes|{{Y}}}}
|
|-
| [[APL (programming language)|APL]]
| {{yes|{{Y}}}}
| Dyalog, ngn and dzaima APL fully support both dfns and tacit functions. GNU APL has rather limited support for dfns.
|-
| [[Assembly language]]s
| {{no|{{N}}}}
|
|-
| [[AutoHotkey|AHK]]
| {{yes|{{Y}}}}
| Since AutoHotkey V2 anonymous functions are supported with a syntax similar to JavaScript.
|-
| [[Bash (Unix shell)|Bash]]
| {{partial|{{Y}}}}
| A library has been made to support anonymous functions in Bash.<ref>{{cite web|title=Bash lambda|website=[[GitHub]]|url=https://github.com/spencertipping/bash-lambda|date=2019-03-08}}</ref>
|-
| [[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#]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|last=BillWagner|title=Lambda expressions - C# reference|url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions|access-date=2020-11-24|website=docs.microsoft.com|language=en-us}}</ref>
|-
| [[C++]]
| {{yes|{{Y}}}}
| As of the [[C++11]] standard
|-
| [[ColdFusion Markup Language|CFML]]
| {{yes|{{Y}}}}
| As of [[Railo]] 4,<ref>{{cite web|title=Closure support|url=http://www.getrailo.org/index.cfm/whats-up/railo-40-beta-released/features/closures/|access-date=2014-01-05|archive-url=https://web.archive.org/web/20140106031957/http://www.getrailo.org/index.cfm/whats-up/railo-40-beta-released/features/closures/|archive-date=2014-01-06|url-status=dead}}</ref> [[ColdFusion]] 10<ref>{{cite web|title=Whats new in ColdFusion 10|url=https://learn.adobe.com/wiki/display/coldfusionen/Whats+new+in+ColdFusion+10|access-date=2014-01-05|archive-url=https://web.archive.org/web/20140106032853/https://learn.adobe.com/wiki/display/coldfusionen/Whats+new+in+ColdFusion+10|archive-date=2014-01-06|url-status=dead}}</ref>
|-
| [[Clojure]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Clojure - Higher Order Functions|url=https://clojure.org/guides/higher_order_functions|access-date=2022-01-14|website=clojure.org}}</ref>
|-
| [[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]]
| {{yes|{{Y}}}}
|
|-
| [[D (programming language)|D]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Functions - D Programming Language|url=https://dlang.org/spec/function.html|access-date=2022-01-14|website=dlang.org}}</ref>
|-
| [[Dart (programming language)|Dart]]
| {{yes|{{Y}}}}
|<ref name=":0">{{Cite web|title=A tour of the Dart language|url=https://dart.dev/guides/language/language-tour|access-date=2020-11-24|website=dart.dev}}</ref>
|-
| [[Delphi (programming language)|Delphi]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Anonymous Methods in Delphi - RAD Studio|url=http://docwiki.embarcadero.com/RADStudio/Sydney/en/Anonymous_Methods_in_Delphi#:~:text=As%20the%20name%20suggests,%20an,a%20parameter%20to%20a%20method.|access-date=2020-11-24|website=docwiki.embarcadero.com}}</ref>
|-
| [[Dylan (programming language)|Dylan]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Functions — Dylan Programming|url=https://opendylan.org/books/dpg/func.html|access-date=2022-01-14|website=opendylan.org}}</ref>
|-
| [[Eiffel (programming language)|Eiffel]]
| {{yes|{{Y}}}}
|
|-
| [[Elm (programming language)|Elm]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=docs/syntax|url=https://elm-lang.org/docs/syntax|access-date=2022-01-14|website=elm-lang.org}}</ref>
|-
| [[Elixir (programming language)|Elixir]]
| {{yes|{{Y}}}}
|<ref name=":1">{{Cite web|title=Erlang/Elixir Syntax: A Crash Course|url=https://elixir-lang.org/crash-course.html|access-date=2020-11-24|website=elixir-lang.github.com|language=en}}</ref>
|-
| [[Erlang (programming language)|Erlang]]
| {{yes|{{Y}}}}
|<ref name=":2">{{Cite web|title=Erlang -- Funs|url=https://erlang.org/doc/programming_examples/funs.html|access-date=2020-11-24|website=erlang.org}}</ref>
|-
| [[F Sharp (programming language)|F#]]
| {{yes|{{Y}}}}
|<ref name=":3">{{Cite web|last=cartermp|title=Lambda Expressions: The fun Keyword - F#|url=https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword|access-date=2020-11-24|website=docs.microsoft.com|language=en-us}}</ref>
|-
| [[Microsoft Excel|Excel]]
| {{yes|{{Y}}}}
| Excel worksheet function, 2021 beta release<ref name="Microsoft Excel Blog 2021-01-25">{{Cite web|title=LAMBDA: The ultimate Excel worksheet function
|url=https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/|access-date=2021-03-30|website=microsoft.com|date=25 January 2021|language=en-us}}</ref>
|-
| [[Factor (programming language)|Factor]]
| {{yes|{{Y}}}}
| "Quotations" support this<ref>{{cite web|title=Quotations - Factor Documentation|url=http://docs.factorcode.org/content/article-quotations.html|access-date=26 December 2015|quote=A quotation is an anonymous function (a value denoting a snippet of code) which can be used as a value and called using the Fundamental combinators.}}</ref>
|-
| [[Fortran]]
| {{no|{{N}}}}
|
|-
| [[Frink (programming language)|Frink]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Frink|url=https://frinklang.org/|access-date=2020-11-24|website=frinklang.org}}</ref>
|-
| [[Go (programming language)|Go]]
| {{yes|{{Y}}}}
|<ref name=":4">{{Cite web|title=Anonymous Functions in GoLang|url=https://golangdocs.com/anonymous-functions-in-golang|access-date=2020-11-24|website=GoLang Docs|date=9 January 2020 |language=en-US}}</ref>
|-
| [[Gosu (programming language)|Gosu]]
| {{yes|{{Y}}}}
| <ref>{{cite web|title=Gosu Documentation|url=http://gosu-lang.org/doc/pdf/gosuref.pdf|access-date=4 March 2013}}</ref>
|-
| [[Groovy (programming language)|Groovy]]
| {{yes|{{Y}}}}
| <ref>{{cite web|title=Groovy Documentation|url=http://groovy.codehaus.org/Closures|access-date=29 May 2012|archive-url=https://web.archive.org/web/20120522213410/http://groovy.codehaus.org/Closures|archive-date=22 May 2012|url-status=dead}}</ref>
|-
| [[Haskell (programming language)|Haskell]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Anonymous function - HaskellWiki|url=https://wiki.haskell.org/Anonymous_function|access-date=2022-01-14|website=wiki.haskell.org}}</ref>
|-
| [[Haxe]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Lambda|url=https://haxe.org/manual/std-Lambda.html|access-date=2022-01-14|website=Haxe - The Cross-platform Toolkit}}</ref>
|-
| [[Java (programming language)|Java]]
| {{yes|{{Y}}}}
| Supported since [[Java 8]].
|-
| [[JavaScript]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Functions - JavaScript {{!}} MDN|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions|access-date=2022-01-14|website=developer.mozilla.org|language=en-US}}</ref>
|-
| [[Julia (programming language)|Julia]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Functions · The Julia Language|url=https://docs.julialang.org/en/v1/manual/functions/|access-date=2020-11-24|website=docs.julialang.org}}</ref>
|-
| [[Kotlin (programming language)|Kotlin]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Higher-Order Functions and Lambdas - Kotlin Programming Language|url=https://kotlinlang.org/docs/reference/lambdas.html|access-date=2020-11-24|website=Kotlin|language=en}}</ref>
|-
| [[Lisp (programming language)|Lisp]]
| {{yes|{{Y}}}}
|
|-
| [[Logtalk]]
| {{yes|{{Y}}}}
|
|-
| [[Lua (programming language)|Lua]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Programming in Lua : 6|url=https://www.lua.org/pil/6.html|access-date=2020-11-24|website=www.lua.org}}</ref>
|-
| [[MUMPS (programming language)|MUMPS]]
| {{no|{{N}}}}
|
|-
| [[Maple (software)|Maple]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Maple Programming: 1.6: Anonymous functions and expressions - Application Center|url=https://www.maplesoft.com/applications/view.aspx?sid=1522&view=html|access-date=2020-11-24|website=www.maplesoft.com}}</ref>
|-
| [[MATLAB]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Anonymous Functions - MATLAB & Simulink|url=https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html|access-date=2022-01-14|website=www.mathworks.com}}</ref>
|-
| [[Maxima (software)|Maxima]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Maxima 5.17.1 Manual: 39. Function Definition|url=http://maths.cnam.fr/Membres/wilk/MathMax/help/Maxima/maxima_39.html#:~:text=Maxima%20simplifies%20funmake%20%27s%20return%20value.&text=Defines%20and%20returns%20a%20lambda,of%20the%20function%20is%20expr_n%20.|access-date=2020-11-24|website=maths.cnam.fr}}</ref>
|-
| [[Nim (programming language)|Nim]]
| {{yes|{{Y}}}}
|<ref name="auto1">{{Cite web|url=https://nim-lang.github.io/Nim/manual.html#procedures-anonymous-procs|title=Nim Manual|website=nim-lang.github.io}}</ref>
|-
| [[OCaml]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Code Examples – OCaml|url=https://ocaml.org/learn/taste.html|access-date=2020-11-24|website=ocaml.org}}</ref>
|-
| [[GNU Octave|Octave]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=GNU Octave: Anonymous Functions|url=https://octave.org/doc/v4.0.1/Anonymous-Functions.html|access-date=2020-11-24|website=octave.org}}</ref>
|-
| [[Object Pascal]]
| {{partial|{{Y}}}}
| Delphi, a dialect of Object Pascal, supports anonymous functions (formally, ''anonymous methods'') natively since Delphi 2009. The [[Oxygene (programming language)|Oxygene]] Object Pascal dialect also supports them.
|-
| [[Objective-C]] (Mac OS X 10.6+)
| {{yes|{{Y}}}}
| Called [[Blocks (C language extension)|blocks]]; in addition to Objective-C, blocks can also be used on C and C++ when programming on Apple's platform.
|-
|[[OpenSCAD]]
| {{yes|{{Y}}}}
| Function Literal support was introduced with version 2021.01.<ref>{{cite web |title=Function Literals |url=https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Function_Literals |website=OpenSCAD User Manual |publisher=Wikibooks |access-date=22 February 2021}}</ref>
|-
| [[Pascal (programming language)|Pascal]]
| {{no|{{N}}}}
|
|-
| [[Perl]]
| {{yes|{{Y}}}}
|<ref name=":5">{{Cite web|title=perlsub - Perl subroutines - Perldoc Browser|url=https://perldoc.perl.org/perlsub|access-date=2020-11-24|website=perldoc.perl.org}}</ref>
|-
| [[PHP]]
| {{yes|{{Y}}}}
| As of PHP 5.3.0, true anonymous functions are supported.<ref>{{Cite web|title=PHP: Anonymous functions - Manual|url=https://www.php.net/manual/en/functions.anonymous.php|access-date=2020-11-24|website=www.php.net}}</ref> Formerly, only partial anonymous functions were supported, which worked much like C#'s implementation.
|-
| [[PL/I]]
| {{no|{{N}}}}
|
|-
| [[Python (programming language)|Python]]
| {{partial|{{Y}}}}
| Python supports anonymous functions through the lambda syntax,<ref name=":6">{{Cite web|title=6. Expressions — Python 3.9.0 documentation|url=https://docs.python.org/3/reference/expressions.html|access-date=2020-11-24|website=docs.python.org}}</ref> which supports only expressions, not statements.
|-
| [[R (programming language)|R]]
| {{yes|{{Y}}}}
|
|-
| [[Racket (programming language)|Racket]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=4.4 Functions: lambda|url=https://docs.racket-lang.org/guide/lambda.html|access-date=2020-11-24|website=docs.racket-lang.org}}</ref>
|-
| [[Raku (programming language)|Raku]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Functions|url=https://docs.raku.org/language/functions|access-date=2022-01-14|website=docs.raku.org}}</ref>
|-
| [[Rexx]]
| {{no|{{N}}}}
|
|-
| [[IBM RPG|RPG]]
| {{no|{{N}}}}
|
|-
| [[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]]
| {{yes|{{Y}}}}
| <ref>{{Cite web|title=Closures: Anonymous Functions that Can Capture Their Environment - The Rust Programming Language|url=https://doc.rust-lang.org/book/ch13-01-closures.html|access-date=2022-01-14|website=doc.rust-lang.org}}</ref>
|-
| [[Scala (programming language)|Scala]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Anonymous Functions|url=https://docs.scala-lang.org/overviews/scala-book/anonymous-functions.html|access-date=2022-01-14|website=Scala Documentation}}</ref>
|-
| [[Scheme (programming language)|Scheme]]
| {{yes|{{Y}}}}
|
|-
| [[Smalltalk]]
| {{yes|{{Y}}}}
| Smalltalk's anonymous functions are called [[Smalltalk#Code blocks|blocks]].
|-
| [[Standard ML]]
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Recitation 3: Higher order functions|url=https://www.cs.cornell.edu/courses/cs312/2008sp/recitations/rec03.html|access-date=2022-01-14|website=www.cs.cornell.edu}}</ref>
|-
| [[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]]
| {{yes|{{Y}}}}
|<ref name=":7">{{Cite web|title=Projects/Vala/Tutorial - GNOME Wiki!|url=https://wiki.gnome.org/Projects/Vala/Tutorial|access-date=2020-11-24|website=wiki.gnome.org}}</ref>
|-
| [[Vala (programming language)|Vala]]
| {{yes|{{Y}}}}
|<ref name=":7" />
|-
| [[Visual Basic .NET]] v9
| {{yes|{{Y}}}}
|<ref>{{Cite web|last=KathleenDollard|title=Lambda Expressions - Visual Basic|url=https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions|access-date=2022-01-14|website=docs.microsoft.com|date=15 September 2021 |language=en-us}}</ref>
|-
| [[Visual Prolog]] v 7.2
| {{yes|{{Y}}}}
|<ref>{{Cite web|title=Language Reference/Terms/Anonymous Predicates - wiki.visual-prolog.com|url=https://wiki.visual-prolog.com/index.php?title=Language_Reference/Terms/Anonymous_Predicates|access-date=2022-01-14|website=wiki.visual-prolog.com}}</ref>
|-
| [[Wolfram Language]]
| {{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==
{{Reflist|30em}}
==External links==
* [http://www.deltics.co.nz/blog/?p=48 Anonymous Methods - When Should They Be Used?] (blog about anonymous function in Delphi)
* [http://www.takipiblog.com/2014/01/16/compiling-lambda-expressions-scala-vs-java-8/ Compiling Lambda Expressions: Scala vs. Java 8]
* [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}}
{{DEFAULTSORT:Anonymous Function}}
[[Category:Data types]]
[[Category:Functional programming]]
[[Category:Lambda calculus]]
[[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]]
|