Function literal: Difference between revisions
Appearance
Content deleted Content added
←Redirected page to Anonymous function |
|||
(7 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Anonymous function]] |
|||
{{wikify|February 2007}} |
|||
In [[Computer Science]], a function literal is a [[literal]] for functions. In contrast to the declaration of a function, no name is associated to a function literal and function literals are also referred to as [[Anonymous function]]. Depending on the language, variables in function literals are bound to different scopes: a function only referring to local variables of this function is a [[pure function]], a function referring to the environment is a [[closure]]. Function literals often use [[Lambda calculus|Lambda]]-Notation where the keyword lambda is followed by the arguments of the function and then the code used to evaluate the function. |
|||
The function literal is useful whenever one function needs to be passed another ([[Higher-order function|higher order]]) function as argument. Examples are the registration of callback functions, sorting functions or functions like [[Map (higher-order function)|map]] or reduce. |
|||
= Examples of function literals = |
|||
[[Lisp (programming language)|Lisp]] |
|||
(lambda (a b) (* a b)) ; function literal |
|||
((lambda (a b) (* a b)) 4 5) ; function is passed 4 and 5 |
|||
[[ECMAScript]] |
|||
function(message) { print(message); } // function literal |
|||
SomeObject.SomeCallBack=function(message) { print(message); } // function literal assigned to callback |
|||
Note that the ''callee'' keyword in ECMAScript makes it possible to write recursive functions without naming them. |
|||
[[Python (programming language)|Python]] |
|||
Python does not have a function literal, because lambda can only be followed an expression and not statements. |
|||
lambda x:x*x # function literal |
|||
map(lambda x:x*x,range(1:10)) # array of first ten square numbers |
Latest revision as of 22:36, 28 April 2010
Redirect to: