Simple inline functions

Thin arrow notation

For brevity, in an inline function declaration, the keyword function can be replaced with the symbol ->. For example, function($a, $b){$a + $b} can be written as ->($a, $b){$a + $b}. The syntax is borrowed from Raku (formerly Perl 6), and is reminiscent of lambda expressions in other languages: (a, b) -> {a + b} in Java, (a, b) => {a + b} in C#.

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.

Argument types and return types can be included in the same way as with the full function syntax, though the benefits of conciseness typically apply only when these are omitted.

A zero-arity function can be written as, for example, ->(){doc('abc.xml')//z}.

Context item functions

Simple inline functions taking a single argument can be simplified even further. For example, the expression ->{@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, (), ->{@salary})

Simple inline functions can access externally-defined local variables in the usual way (that is, they have a closure).

The expression ->{EXPR} is a syntactic shorthand for:

function($x as item()) as item()* {$x!(EXPR)}

Underscore functions