This module adds some useful higher-order functions that were left out of the official spec. All functions are introduced with the hof:
prefix, which is linked to the statically declared http://basex.org/modules/hof
namespace.
Functions
hof:id
Signatures
|
hof:id($expr as item()*) as item()*
|
Summary
|
Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions.
|
Examples
|
hof:id(1 to 5) returns 1 2 3 4 5
|
hof:const
Signatures
|
hof:const($expr as item()*, $ignored as item()*) as item()*
|
Summary
|
Returns its first argument unchanged and irgores the second. This function isn't useful on its own, but can be used as argument to other higher-order functions.
|
Examples
|
hof:const(42, 1337) returns 42 .
|
hof:fold-left1
Signatures
|
hof:fold-left1($f as function(item()*, item()) as item()*, $seq as item()+) as item()*
|
Summary
|
Works the same as fn:fold-left($f, $seed, $seq), but doesn't need a seed, because the sequence must be non-empty.
|
Errors
|
XPTY0004 if $seq is empty
|
Examples
|
hof:fold-left1(function($a, $b) { $a + $b }, 1 to 10) returns 55 .
hof:fold-left1(function($a, $b) { $a + $b }, ()) throws XPTY0004 , because $seq has to be non-empty.
|
hof:until
Signatures
|
hof:until($pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as item()*
|
Summary
|
Applies the function $f to the initial value $start until the predicate $pred applied to the result returns true() .
|
Examples
|
hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1) returns 1024 .
- Calculating the square-root of a number by iteratively improving an initial guess:
let $sqrt := function($x as xs:double) as xs:double {
hof:until(
function($res) { abs($res * $res - $x) < 0.00001 },
function($guess) { ($guess + $x div $guess) div 2 },
$x
)
}
return $sqrt(25)
returns 5.000000000053722 .
|
hof:top-k-by
Signatures
|
hof:top-k-by($sort-key as function(item()) as item(), $k as xs:integer, $seq as item()*) as item()*
|
Summary
|
Returns the $k items in $seq that are greatest when sorted by the result of $f applied to the item. The function is a much more efficient implementation of the following scheme:
(
for $x in $seq
order by $sort-key($x)
return $x
)[position() <= $k]
|
Errors
|
XPTY0004 if $sort-key doesn't return exactly one item
|
Examples
|
hof:top-k-by(hof:id#1, 5, 1 to 1000) returns 1000 999 998 997 996
hof:top-k-by(function($x) { -$x }, 3, 1 to 1000) returns 1 2 3
hof:top-k-by(xs:integer#1, 2, <x a='1' b='2' c='3'/>/@*)/node-name() returns c b
|
hof:top-k-with
Signatures
|
hof:top-k-with($lt as function(item(), item()) as xs:boolean, $k as xs:integer, $seq as item()*) as item()*
|
Summary
|
Returns the $k items in $seq that are greatest when sorted in the order of the less-than predicate $lt . The function is a general version of hof:top-k-by($sort-key, $k, $seq) .
|
Examples
|
hof:top-k-with(function($a, $b) { $a lt $b }, 5, 1 to 1000) returns 1000 999 998 997 996
hof:top-k-with(function($a, $b) { abs($a) gt abs($b) }, 5, -5 to 5) returns 0 1 -1 2 -2
|
Recent Changes
- removed
hof:iterate
- added
hof:top-k-by
and hof:top-k-with