where $item instance of function(*)
return fn:function-arity($item)
</syntaxhighlightpre>
''Result:'' <code>3 1</code>
fn:substring($str, $pos, 1)
};
</syntaxhighlightpre>
for example has the type <code>function(xs:string, xs:integer) as xs:string</code>. It isn't possible to specify only the argument and not the result type or the other way round. A good place-holder to use when no restriction is wanted is <code>item()*</code>, as it matches any XQuery value.
fn:for-each($fun, ?)
};
</syntaxhighlightpre>
We willl see later how <code>fn:for-each(...)</code> works. The type of <code>local:on-sequences(...)</code> on the other hand is easily constructed, if a bit long:
<syntaxhighlight lang="xquery">
fn:for-each(1 to 10, math:pow(?, 2))
</syntaxhighlightpre>
''Result:'' <code>1 4 9 16 25 36 49 64 81 100</code>
</li>
)
return fn:for-each($fs, function($f) { $f('foobar') })
</syntaxhighlightpre>
''Result:'' <code>FOOBAR bar 6</code>
</li>
<syntaxhighlight lang="xquery">
("one", "two", "three") => fn:for-each(fn:upper-case(?))
</syntaxhighlightpre>
''Result:'' <code>ONE TWO THREE</code>
</li></ul>
return $fun($s)
};
</syntaxhighlightpre>
|}
<syntaxhighlight lang="xquery">
fn:filter(1 to 10, function($x) { $x mod 2 eq 0 })
</syntaxhighlightpre>
''Result:'' <code>2 4 6 8 10</code>
</li>
}
return fn:filter(('FooBar', 'foo', 'BAR'), $first-upper)
</syntaxhighlightpre>
''Result:'' <code>FooBar BAR</code>
</li>
}
return filter(1 to 20, $is-prime)
</syntaxhighlightpre>
''Result:'' <code>2 3 5 7 11 13 17 19</code>
</li></ul>
)
};
</syntaxhighlightpre>
|- valign="top"
| '''XQuery 1.0'''
$seq[$pred(.)]
};
</syntaxhighlightpre>
|}
function($a, $b) { $a + $b }
)
</syntaxhighlightpre>
''Result:'' <code>2 1 2 1 2</code>
</li>
}
return $number-words('how are you?')
</syntaxhighlightpre>
''Result:''
<syntaxhighlight lang="xquery">
2: are
3: you?
</syntaxhighlightpre>
</li>
<li>Checking if a sequence is sorted:
$is-sorted((1, 2, 42, 4, 5))
)
</syntaxhighlightpre>
''Result:'' <code>true false</code></li></ul>
|- valign="top"
return $fun($seq1[$pos], $seq2[$pos])
};
</syntaxhighlightpre>
|}
return result;
}
</syntaxhighlightpre>
Nice and efficient implementations using folds will be given below.
<syntaxhighlight lang="xquery">
$f($f($f($f($f(0, 1), 2), 3), 4), 5)
</syntaxhighlightpre>
|- valign="top"
| '''Examples'''
function($result, $curr) { $result * $curr }
)
</syntaxhighlightpre>
''Result:'' <code>120</code>
</li>
concat('$f(', ?, ', ', ?, ')')
)
</syntaxhighlightpre>
''Result:'' <code>$f($f($f($f($f($seed, 1), 2), 3), 4), 5)</code>
</li>
$from-digits((4, 2))
)
</syntaxhighlightpre>
''Result:'' <code>12345 42</code>
</li></ul>
)
};
</syntaxhighlightpre>
|}
<syntaxhighlight lang="xquery">
$f(1, $f(2, $f(3, $f(4, $f(5, 0)))))
</syntaxhighlightpre>
|- valign="top"
| '''Examples'''
function($curr, $result) { $result * $curr }
)
</syntaxhighlightpre>
''Result:'' <code>120</code>
</li>
concat('$f(', ?, ', ', ?, ')')
)
</syntaxhighlightpre>
''Result:'' <code>$f(1, $f(2, $f(3, $f(4, $f(5, $seed)))))</code>
</li>
)
return $reverse(1 to 10)
</syntaxhighlightpre>
''Result:'' <code>10 9 8 7 6 5 4 3 2 1</code>
</li></ul>
)
};
</syntaxhighlightpre>
Note that the order of the arguments of <code>$fun</code> are inverted compared to that in <code>fn:fold-left(...)</code>.
|}