This page talks about ''higher-order functions'' introduced with [[XQuery 3.0]]. The BaseX-specific <code>hof</code> module containing some more very usful functions can be found at [[Higher-Order Functions Module]].
Warning: in the new version of the [http://www.w3.org/TR/xpath-functions-30/#func-filter XQuery Functions and Operators] specification, some function signatures have been changed. All function arguments are now placed last:
=Function Items=
with their possible XQuery implementation and some motivating examples.
===fn:map($f, $seq)for-each=== {{Mark|Updated with Version 7.7:}} the function has been renamed, and the arguments have been swapped.<br/>
{|
|-
| width='90' | '''Signatures'''
|<code><b>fn:map</b>($seq as item()*, $f as function(item()) as item()*) as item()*</code><br/><font color='gray'>Old signature: fn:map($f as function(item()) as item()*, $seq as item()*) as item()*</code><br /font>
|-
| '''Summary'''
<ul><li>Squaring all numbers from 1 to 10:
<pre class="brush:xquery">
fn:map(1 to 10, math:pow(?, 2), 1 to 10)
</pre>
''Result:'' <code>1 4 9 16 25 36 49 64 81 100</code>
fn:string-length#1
)
return fn:map($fs, function($f) { $f('foobar') }, $fs)
</pre>
''Result:'' <code>FOOBAR bar 6</code>
|}
===fn:filter($pred, $seq)=== {{Mark|Updated with Version 7.7:}} the arguments have been swapped.<br/>
{|
|-
| width='90' | '''Signatures'''
|<code><b>fn:filter</b>($seq as item()*, $pred as function(item()) as xs:boolean) as item()*</code><br /><font color='gray'>fn:filter($pred as function(item()) as xs:boolean, $seq as item()*) as item()*</code><br /font>
|-
| '''Summary'''
|<ul><li>All even integers until 10:
<pre class="brush:xquery">
fn:filter(1 to 10, function($x) { $x mod 2 eq 0 }, 1 to 10)
</pre>
''Result:'' <code>2 4 6 8 10</code>
return $first eq fn:upper-case($first)
}
return fn:filter($first-upper, ('FooBar', 'foo', 'BAR'), $first-upper)
</pre>
''Result:'' <code>FooBar BAR</code>
$x gt 1 and (every $y in 2 to ($x - 1) satisfies $x mod $y ne 0)
}
return filter(1 to 20, $is-prime, 1 to 20)
</pre>
''Result:'' <code>2 3 5 7 11 13 17 19</code>
declare function local:filter($pred, $seq) {
map(
$seq,
function($x) {
if($pred($x)) then $x else ()
}, $seq
)
};
|}
===fn:mapfor-pairs($f, $seq1, $seq2)each-pair=== {{Mark|Updated with Version 7.7:}} the function has been renamed, and the arguments have been swapped.<br/>
{|
|-
| width='90' | '''Signatures'''
|<code><b>fn:mapfor-each-pairspair</b>($seq1 as item()*, $seq2 as item()*, $f as function(item(), item()) as item()*) as item()*</code><br /><font color='gray'>Old signature: fn:map-pairs($f as function(item(), item()) as item()*, $seq1 as item()*, $seq2 as item()*) as item()*</code><br /font>
|-
| '''Summary'''
<pre class="brush:xquery">
fn:map-pairs(
fn:map(1 to 10, function($x) { $x mod 2 }),
(1, 1, 1, 1, 1),
function($a, $b) { $a + $b },
fn:map(function($x) { $x mod 2 }, 1 to 10),
(1, 1, 1, 1, 1)
)
</pre>
fn:string-join(
fn:map-pairs(
concat(?, ': ', ?),
1 to 1000,
tokenize($str, '\r?\n|\r'), concat(?, ': ', ?)
),
'&#xa;'
every $b in
fn:map-pairs(
function($a, $b) { $a le $b },
$seq,
fn:tail($seq) , function($a, $b) { $a le $b }
)
satisfies $b