Lambda Expression

Lambda expression is a function definition with only one statement.

Syntax

def <functionName>(parameters): expression

or

def (parameters): expression

or

def (parameters) -> expression

or

parameter -> expression

Details

Lambda expression can be either a named function or anonymous function. It takes any number of parameters but has only one expression. The second, third, and fourth of the aforementioned syntax define an anonymous Lambda function and the fourth syntax only applies to a statement with one parameter. An anonymous Lambda function can be used in the following ways.

  1. Assigned to a variable, e.g. f=def(x):x*x.

  2. Used as the parameter to a function, e.g. each(def(a,b):a+b, 1..10, 2..11).

  3. Used as the return value of a function, e.g. def(x){return def(k): k*x}.

Examples

def f(x):x pow 2 + 3*x + 4.0;
f(2);
// output
14

def orderby(x,y): x[isort y];
t=table(3 1 7 as id, 7 4 9 as value);
orderby(t, t.id);
id value
1 4
3 7
7 9
each(def(a,b):a+b, 1..10, 2..11);
// output
[3,5,7,9,11,13,15,17,19,21]

g = def (x,y) -> log(x) + y pow 2;
g(e,0);
// output
1

each(x->x+1,1 3 5)
// output
[2,4,6]