Changes

Jump to navigation Jump to search
1,047 bytes added ,  10:29, 21 August 2020
no edit summary
|-
| '''Summary'''
|Applies the predicate function {{Code|$fpred}} to the initial value {{Code|$start}} until . If the predicate result is {{Code|false}}, {{Code|$predf}} applied to is invoked with the start value – or, subsequently, with the result of this function – until the predicate function returns {{Code|true()}}.
|-
| '''Examples'''
|
* {{Code|Doubles a numeric value until a maximum is reached:<syntaxhighlight lang="xquery">hof:until( function($xoutput) { $x output ge 1000 }, function($yinput ) { 2 * $y input }, 1)}} returns {{Code|1024}}.</syntaxhighlight>* Calculating Calculates the square-root of a number by iteratively improving an initial guess:<pre classsyntaxhighlight lang="brush:xquery">let $sqrt := function($x input as xs:double) as xs:double {
hof:until(
function($resresult) { abs($res result * $res result - $xinput) < 0.00001 }, function($guess) { ($guess + $x input div $guess) div 2 }, $xinput
)
}
return $sqrt(25)
</presyntaxhighlight>returns * Returns {{Code|5.000000000053722OK}}. * The evaluation of , as the predicate is pre-fixed, thus every cycle starts with the evaluation of the predicate evaluated first.:<pre classsyntaxhighlight lang="brush:xquery">declare hof:until( function local:pred($res_) { true() };,declare function local:loop($val_) { $val + 1 error() };, 'OK'hof:until(local:pred#1, local:loop#1, 0)</presyntaxhighlight>returns {{Code|0}}. 
|}
| '''Summary'''
|This function is similar to [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but it returns a list of successive reduced values from the left. It is equivalent to:
<pre classsyntaxhighlight lang="brush:xquery">
declare function hof:scan-left($seq, $acc, $f) {
if(empty($seq)) then $acc else (
)
};
</presyntaxhighlight>
|-
| '''Examples'''
|
* Returns triangular numbers:
<pre classsyntaxhighlight lang="brush:xquery">
hof:scan-left(1 to 10, 0, function($a, $b) { $a + $b })
</presyntaxhighlight>
|}
| '''Summary'''
|The function returns items of <code>$seq</code> as long as the predicate <code>$pred</code> is satisfied. It is equivalent to:
<pre classsyntaxhighlight lang="brush:xquery">
declare function hof:take-while($seq, $pred) {
if(empty($seq) or not($pred(head($seq)))) then () else (
)
};
</presyntaxhighlight>
|-
| '''Examples'''
|
* Computes at most 100 random integers, but stops if an integer is smaller than 10:
<pre classsyntaxhighlight lang="brush:xquery">
hof:take-while(
(1 to 100) ! random:integer(50),
function($x) { $x >= 10 }
)
</presyntaxhighlight>|} ==hof:drop-while== {{Mark|Introduced with Version 9.5.}} {| width='100%'|-| width='120' | '''Signatures'''|{{Func|hof:drop-while|$seq as item()*, $pred as function(item()) as xs:boolean|item()*}}|-| '''Summary'''|The function skips all items of <code>$seq</code> until the predicate <code>$pred</code> is not satisfied anymore. It is equivalent to:<syntaxhighlight lang="xquery">declare function hof:drop-while($seq, $pred) { if($pred(head($seq))) then ( hof:drop-while(tail($seq), $pred) ) else ( $seq )};</syntaxhighlight>|-| '''Examples'''|Returns the name of the first file that does not exist on disk:<syntaxhighlight lang="xquery">hof:drop-while( (1 to 1000) ! (. || '.log'), file:exists#1)[1]</syntaxhighlight>
|}
| '''Summary'''
|Returns the {{Code|$k}} items in {{Code|$seq}} that are greatest when sorted by the result of {{Code|$f}} applied to the item. The function is a much more efficient implementation of the following scheme:
<pre classsyntaxhighlight lang="brush:xquery">
(for $x in $seq
order by $sort-key($x) descending
return $x
)[position() <= $k]
</presyntaxhighlight>
|-
| '''Examples'''
|-
| '''Summary'''
|Returns its argument unchanged. This function isn't isn’t useful on its own, but can be used as argument to other higher-order functions.
|-
| '''Examples'''
* {{Code|hof:id(1 to 5)}} returns {{Code|1 2 3 4 5}}
* With higher-order functions:
<pre classsyntaxhighlight lang="brush:xquery">let $sort := sort(?, (), hof:id#1)let $reverse-sort := sort(?, (), function($x) { -$x })
return (
$sort((1, 5, 3, 2, 4)),
$reverse-sort((1, 5, 3, 2, 4))
)
</presyntaxhighlight>
returns: <code>1 2 3 4 5 | 5 4 3 2 1</code>
|}
|-
| '''Summary'''
|Returns its first argument unchanged and ignores the second. This function isn't isn’t useful on its own, but can be used as argument to other higher-order functions, e.g. when a function combining two values is expected and one only wants to retain the left one.
|-
| '''Examples'''
* {{Code|hof:const(42, 1337)}} returns {{Code|42}}.
* With higher-order functions:
<pre classsyntaxhighlight lang="brush:xquery">
let $zip-sum := function($f, $seq1, $seq2) {
sum(for-each-pair($seq1, $seq2, $f))
$sum-left((1, 1, 1, 1, 1), 1 to 5)
)
</presyntaxhighlight>
* Another use-case: When inserting a key into a map, {{Code|$f}} decides how to combine the new value with a possibly existing old one. {{Code|hof:const}} here means ignoring the old value, so that's normal insertion.
<pre classsyntaxhighlight lang="brush:xquery">
let $insert-with := function($f, $map, $k, $v) {
let $old := $map($k)
$ins($map, 'foo', 42)('foo')
)
</presyntaxhighlight>
returns {{Code|3 42}}
|}
=Changelog=
 
;Version 9.5
* Added: [[#hof:drop-while|hof:drop-while]]
;Version 8.1
 
* Added: [[#hof:scan-left|hof:scan-left]], [[#hof:take-while|hof:take-while]]
;Version 7.2
 
* Added: [[#hof:top-k-by|hof:top-k-by]], [[#hof:top-k-with|hof:top-k-with]]
* Removed: hof:iterate
;Version 7.0
 
* module added
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu