The apply-to-each construct is used to apply any function over the elements of a sequence. It has the following syntax:
An apply-to-each construct consists of three parts: the expression before the colon, which we will call the body, the bindings that follow the body, and the expression that follows the |, which we will call the sieve. Both the body and the sieve are optional: they could both be left out, as in
The rbinds can contain multiple bindings which are separated by semicolons. We first consider the case in which there is a single binding. A binding can either consist of a pattern followed by the keyword IN and an expression (full binding), or consist of a variable name (shorthand binding). In a full binding the expression is evaluated (it must evaluate to a sequence) and the variables in the pattern are bound in turn to each element of the sequence. The body and sieve are applied for each of these bindings. For example:
In a shorthand binding, the variable must be a sequence, and the body and sieve are applied to each element of the sequence with the variable name bound to the element. For example:
In the case of multiple rbinds, each of the sequences (either the result of the expression in a full binding or the value of the variable in a shorthand binding) must be of equal length. The bindings are interleaved so that the body is evaluated with bindings made for elements at the same index of each sequence. For example:
An apply-to-each with a body and two bindings,
{body: pattern1 in exp1; pattern2 in exp2 | sieve}
is equivalent to the single binding construct
{body: (pattern1,pattern2) in zip(exp1,exp2) | sieve}
where zip, as defined in the list of functions, elementwise zips together the two sequences it is given as arguments.
If there is no body in an apply-to-each construct, then the results of the first binding is returned. For example:
If there is a body and a sieve, the body and sieve are both evaluated for all bindings, and then the subselection is applied. An apply-to-each with a sieve of the form:
{body : bindings | sieve}
is equivalent to the construct
pack({(body,sieve) : bindings})