XPath/XQuery for-each-pair function

Summary

Returns an array obtained by evaluating the supplied function once for each pair of members at the same position in the two supplied arrays.

Signature

array:for-each-pair(
$array1 as array(*),
$array2 as array(*),
$function as function(item()*, item()*) as item()*
) as array(*)

Properties

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

Rules

Returns the result of the recursive expression:

				
if (array:size($array1) eq 0 or array:size($array2) eq 0)
then [ ]
else array:concat(
        $function(array:head($array1), array:head($array2)), 
        array:for-each-pair(array:tail($array1), array:tail($array2), $function)
     )
         
			

Examples

The expression array:for-each-pair(["A", "B", "C"], [1, 2, 3], function($x, $y) { array {$x, $y}}) returns [["A", 1], ["B", 2], ["C", 3]].

The expression let $A := ["A", "B", "C", "D"] return array:for-each-pair($A, array:tail($A), concat#2) returns ["AB", "BC", "CD"].

Notes

If the arrays have different size, excess members in the longer array are ignored.