array:fold-left

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 )
         

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.

Examples

The expression array:fold-left([true(), true(), false()], true(), function($x, $y){$x and $y}) returns false(). (Returns true if every member of the input array has an effective boolean value of true().)

The expression array:fold-left([true(), true(), false()], false(), function($x, $y){$x or $y}) returns true(). (Returns true if at least one member of the input array has an effective boolean value of true().)

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