fn:for-each

Applies the function item $action to every item from the sequence $seq in turn, returning the concatenation of the resulting sequences in order.

Signature

fn:for-each(
    $seq as item()*, 
    $action as function(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 implementation in XQuery:

declare function fn:for-each($seq, $action) {
  if (fn:empty($seq))
  then ()
  else ($action(fn:head($seq)), fn:for-each(fn:tail($seq), $action))
};

or its equivalent in XSLT:

<xsl:function name="fn:for-each">
  <xsl:param name="seq"/>
  <xsl:param name="action"/>
  <xsl:if test="fn:exists($seq)">
    <xsl:sequence select="$action(fn:head($seq)), fn:for-each(fn:tail($seq), $action)"/>
  </xsl:if>
</xsl:function>
         

Notes

The function call fn:for-each($SEQ, $F) is equivalent to the expression for $i in $SEQ return $F($i), assuming that ordering mode is ordered.

Examples

The expression fn:for-each(1 to 5, function($a) { $a * $a }) returns (1, 4, 9, 16, 25).

The expression fn:for-each(("john", "jane"), fn:string-to-codepoints#1) returns (106, 111, 104, 110, 106, 97, 110, 101).

The expression fn:for-each(("23", "29"), xs:int#1) returns (23, 29).