An anonymous function is a function (or a subroutine) defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous. The Y combinator can be utilised in these circumstances to provide anonymous recursion. Certain programming languages also provide support for both named and anonymous functions. The lambda calculus without anonymous function definition forms a combinatory logic.
Some object-oriented programming languages have anonymous classes, which are a similar concept. Java is such a language.
Uses
Anonymous functions can be used to contain functionality that need not be named and possibly for short-term use. Some notable examples include closures and currying.
Sorting
When attempting to sort in a non-standard way it may be easier to contain the comparison logic as an anonymous function instead of creating a named function.
In python, consider sorting items in a list by the name of their class:
a = [10, '10', 10.0]
a.sort(lambda x,y: cmp(x.__class__.__name__, y.__class__.__name__))
which results in
[10.0, 10, '10']
Note that 10.0 has class name "float", 10 has class name "int", and '10' has class name "str". The sorted order is "float", "int", then "str".
The anonymous function in this example is the lambda expression:
lambda x,y: cmp(...)
The anonymous function accepts two argument — x & y — and returns the comparison between them using the built-in function cmp(). Another example would be sorting a list of strings by length of the string:
a = ['three', 'two', 'four']
a.sort(lambda x,y: cmp(len(x), len(y)))
which results in
['two', 'four', 'three']
which clearly has by sorted by length of the strings.
Closures
Closures are functions evaluated in an environment containing bound variables. The following example binds the variable "threshold" in an anonymous function that compares the input to the threshold.
def comp(threshold):
return lambda x: x < threshold
This can be used as a sort of generator of comparison functions:
a = comp(10)
b = comp(20)
print a(9), a(10), a(20), a(21)
True False False False
print b(9), b(10), a(20), b(21)
True True False False
It would clearly be very 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 what the contains the functionality that does the comparing.
Currying
Currying is transforming a function from multiple inputs to fewer inputs.
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 10
print half(40), third(40)
20 13
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.
List of languages
This list is incomplete; you can help by adding missing items. |
Language | Full support | Some support | No support |
---|---|---|---|
C | X | ||
C++ | X | ||
C# v1 | X | ||
C# v2 | X | ||
Java | X | ||
JavaScript | X | ||
Lisp | X | ||
Perl | X | ||
Python | X | ||
PHP | X | ||
Scheme | X |
Examples
Numerous languages support anonymous functions, or something similar.
JavaScript
JavaScript supports anonymous functions.
var foo = function(x) {return x*x;}
alert(foo(10));
Unlike python, anonymous functions in JavaScript are just like named functions and are declared just like named functions.
List
Lisp supports anonymous functions.
(lambda (arg) (* arg arg))
Python
Python supports anonymous functions through the lambda form. It is, however, expected to be only a single line of code and always returns whatever that line returns. For example:
foo = lambda x: x*x
print foo(10)
The lambda function always returns x*x and there is no way for a lambda function to not return something. This makes anonymous functions limited and are not simply nameless functions.
PHP
PHP doesn't have true anonymous functions because the only way to reference functions is by name. The closest PHP is shown in the following.
$foo = create_function("$x", "return $x*$x;");
echo $foo(10);