XPath/XQuery fold-right function

Summary

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) 
              )
         

Examples

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

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

The expression array:fold-right([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 $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.