Higher-Order Functions
This module adds some useful higher-order functions, in addition to the standard Higher-Order Functions.
The following functions have been incorporated into the official specification:
| BaseX 10 | XQuery 4 |
hof:drop-while |
fn:subsequence-where |
hof:id, hof:const |
fn:identity |
hof:until |
fn:while-do, fn:do-until |
hof:take-while |
fn:take-while |
Conventions
All functions are in the http://basex.org/modules/hof namespace, to which the hof prefix is statically bound.
Loops
hof:fold-left1
| Signature | hof:fold-left1( $input as item()+, $action as fn(item()*, item()) as item()*) as item()* |
|---|---|
| Summary | Works the same as fn:fold-left, but does not need a seed, because the sequence must be non-empty. |
| Examples | Result: 55Raises XPTY0004, because $input has to be non-empty. |
Sorting
hof:top-k-by
| Signature | hof:top-k-by( $input as item()*, $key as fn(item()) as item(), $k as xs:integer) as item()* |
|---|---|
| Summary | Returns the $k items in $input that are greatest when sorted by the result of $key applied to the item. The function is a much more efficient implementation of the following scheme:
|
| Examples | Result: 1000, 999, 998, 997, 996 Result: 1, 2, 3 Result: 'c', 'b' |
hof:top-k-with
| Signature | hof:top-k-with( $input as item()*, $comparator as fn(item(), item()) as xs:boolean, $k as xs:integer) as item()* |
|---|---|
| Summary | Returns the $k items in $input that are greatest when sorted in the order of the less-than predicate $comparator. The function is a general version of hof:top-k-by. |
| Examples | Result: 1000, 999, 998, 997, 996 Result: 0, 1, -1, 2, -2 |
Changelog
Version 13.0- Removed:
hof:scan-left(replaced withfn:scan)
- Removed:
hof:until(replaced withfn:while-do/fn:do-until),hof:id/hof:const(replaced withfn:identity),hof:drop-while(replaced withfn:subsequence-where),hof:take-while(replaced withfn:take-while)
- Added:
hof:drop-while
- Added:
hof:scan-left,hof:take-while
- Added:
hof:top-k-by,hof:top-k-with - Removed: hof:iterate
- Added: New module added.
⚡Generated with XQuery