array:fold-right

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

Signature

array:fold-right(
    $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 $function( array:head($array), 
                array:fold-right(array:tail($array), $zero, $function) 
              )
         

Notes

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

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

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

Examples

The expression array:fold-right([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-right([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-right([1,2,3], [], function($x, $y){[$x, $y]}) returns [1, [2, [3, []]]].