XPath/XQuery fold-left function

Summary

Evaluates the supplied function cumulatively on successive members of the supplied array.

Signature

array:fold-left(
$array as array(*),
$zero as item()*,
$function as function(item()*, item()*) as item()*
) as item()*

Properties

This function is deterministic, context-independent, focus-independent, and higher-order.

Rules

The effect of the function is equivalent to the following recursive definition:

if (array:size($array) eq 0)
then $zero
else array:fold-left(array:tail($array), 
                     $function($zero, array:head($array)), 
                     $function )
         

Examples

The expression array:fold-left([true(), true(), false()], true(), function($x, $y){$x and $y}) returns false().

The expression array:fold-left([true(), true(), false()], false(), function($x, $y){$x or $y}) returns true().

The expression array:fold-left([1,2,3], [], function($x, $y){[$x, $y]}) returns [[[[], 1], 2], 3].

Notes

If the supplied array is empty, the function returns $zero.

If the supplied array contains a single member $m, the function returns $zero => $function($m).

If the supplied array contains two members $m and $n, the function returns $zero => $function($m) => $function($n); and similarly for an input array with more than two members.