In a programming language, an unnamed function object (also: "function literal").
Example (in PseudoCode):
"lambda(x,y){ x>y }" is an anonymous function object representing the function that tells whether its first argument is greater than its second argument.
This lets you write (e.g.)
sort(lambda(x,y){ x>y }, [5,7,3,4,9,5,4])
instead of
def compfunc(x,y) {
x>y
}
sort(compfunc,[5,7,3,4,9,5,4])
Analogously, "42" is an anonymous number object (or "number literal") representing the number 42, which lets you write (e.g.)
set_age(42)
instead of
def value := 42
set_age(value)
(for the last code snippet, imagine that this was written in a programming language that does not let you use numbers directly, but always requires you to give them a name first using "def <name> := <value>")
The syntax of anonymous functions depends on the programming language being used:
- "lambda x,y: x>y" in PythonLanguage
- "proc{|x,y|x>y}" in RubyLanguage
- "{|x,y| x>y}" in FrinkLanguage
- "sub { my ($x, $y) = @_; $x > $y }" in PerlLanguage
- "(lambda (x y) (> x y))" in LispLanguage and SchemeLanguage
- "function(x,y) {return x>y}" in JavaScript
- "\x y -> x > y" in HaskellLanguage
- "fun x y -> x > y" in ObjectiveCaml
- "fn (x, y) => x > y" in SmlLanguage
- "function($x, $y) { return $x > $y; }" in PhpLanguage 5.3+
- "^(int x, int y) { return x > y; }" in ObjectiveCee (as well as CeeLanguage and CeePlusPlus) on MacOSX 10.6+
- ForthLanguage: the execution token (xt) returned by ":noname ( x y -- ? ) > ;" (or simply " ' > " since operators are also functions)
- [](int x, int y) { return x > y; } in CeePlusPlusEleven
- [:x :y | x > y] in SmallTalk
- "OPERATOR (x INTEGER, y INTEGER) RETURNS BOOLEAN; RETURN x > y; END OPERATOR" in the RelProject. (Yes, it's verbose. Deliberately.)
- "(x, y) => x > y" in CeeSharpLanguage
- "(x, y) -> x > y" in JavaLanguage (Java 8 and above)
Shouldn't this be simply called an unnamed function? --
MarkJanssen
Sometimes it is. In every technical field, science or craft, some things get multiple names.
See also LambdaExpression