Changes

Jump to navigation Jump to search
60 bytes removed ,  18:35, 1 December 2023
m
Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>"
| '''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:
<syntaxhighlight pre lang="'xquery"'>
declare function hof:scan-left($input, $acc, $action) {
if(empty($input)) then $acc else (
|
* Returns triangular numbers:
<syntaxhighlight pre lang="'xquery"'>
hof:scan-left(1 to 10, 0, function($a, $b) { $a + $b })
</pre>
| '''Summary'''
|Returns the {{Code|$k}} items in {{Code|$input}} that are greatest when sorted by the result of {{Code|$key}} applied to the item. The function is a much more efficient implementation of the following scheme:
<syntaxhighlight pre lang="'xquery"'>
(for $item in $input
order by $key($item) descending
* {{Code|hof:const(42, 1337)}} returns {{Code|42}}.
* With higher-order functions:
<syntaxhighlight pre lang="'xquery"'>
let $zip-sum := function($f, $seq1, $seq2) {
sum(for-each-pair($seq1, $seq2, $f))
</pre>
* 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.
<syntaxhighlight pre lang="'xquery"'>
let $insert-with := function($f, $map, $k, $v) {
let $old := $map($k)
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu