Simple inline functions
Lambda notation
New syntax, similar to lambda expresions in Java, C#, and Javascript, is available for
anonymous inline functions. For example, function($a, $b){$a + $b}
can be written as
($a, $b)->{$a + $b}
.
The concise syntax is helpful when writing simple callbacks, for example fold-left($seq, 1, ($x, $y)->{$x*$y})
to calculate the product of a sequence of numbers.
The parentheses can be omitted around the argument list if there is exactly one argument. But note that whitespace is needed between the variable name and the arrow (this is because names can include a hyphen).
A zero-arity function can be written as, for example, ()->{doc('abc.xml')//z}
.
Focus functions
Simple inline functions taking a single argument can be simplified even further. For example,
the expression function{@code}
represents a function that takes a single argument (presumably an element node),
and returns a selected attribute of that node. A simple inline function takes a single argument with required type item()
,
and returns any sequence (type item()*
). The function body is evaluated with a singleton focus based
on the supplied argument value.
Simple inline functions are particularly convenient when providing functions as arguments to higher-order functions, many of which accept a single item as their one argument. For example, to sort employees in order of salary, you can write:
sort(//employee, (), function{@salary})Simple inline functions can access externally-defined local variables in the usual way (that is, they have a closure).
The expression function{EXPR}
is a syntactic shorthand for: