# 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

For functions taking more than one argument, or an argument that is not confined to a single item, an abbreviated syntax ("underscore functions") is provided. For example, the expression `_{\$1 + \$2}` is a function that takes two arguments and returns their sum. It is equivalent to the expression:

function(\$a1 as item()*, \$a2 as item()*) as item()* {\$a1 + \$a2}

The arity of an underscore function is determined by the highest-numbered parameter reference appearing within the function. For example, `_{\$2}` is a function that takes two arguments and returns the value of the second argument.

This syntax can also be used to define a zero-arity function, for example the following is a function that returns tomorrow's date:

_{current-date() + xs:dayTimeDuration('P1D')}