Changes

Jump to navigation Jump to search
1,284 bytes removed ,  16:45, 11 November 2023
no edit summary
* {{Code|hof:fold-left1(1 to 10, function($a, $b) { $a + $b })}} returns {{Code|55}}.
* {{Code|hof:fold-left1((), function($a, $b) { $a + $b })}} throws {{Code|XPTY0004}}, because {{Code|$seq}} has to be non-empty.
|}
 
==hof:until==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>hof:until(
$predicate as function(item()*) as xs:boolean,
$action as function(item()*) as item()*,
$zero as item()*
) as item()*</pre>
|- valign="top"
| '''Summary'''
|Applies the predicate function {{Code|$predicate}} to {{Code|$zero}}. If the result is {{Code|false}}, {{Code|$action}} is invoked with the start value – or, subsequently, with the result of this function – until the predicate function returns {{Code|true()}}.
|- valign="top"
| '''Examples'''
|
* Doubles a numeric value until a maximum is reached:
<syntaxhighlight lang="xquery">
hof:until(
function($output) { $output ge 1000 },
function($input ) { 2 * $input },
1
)
</syntaxhighlight>
* Calculates the square root of a number by iteratively improving an initial guess:
<syntaxhighlight lang="xquery">
let $sqrt := function($input as xs:double) as xs:double {
hof:until(
function($result) { abs($result * $result - $input) < 0.00001 },
function($guess) { ($guess + $input div $guess) div 2 },
$input
)
}
return $sqrt(25)
</syntaxhighlight>
* Returns {{Code|OK}}, as the predicate is evaluated first:
<syntaxhighlight lang="xquery">
hof:until(
function($_) { true() },
function($_) { error() },
'OK'
)
</syntaxhighlight>
|}
=Changelog=
 
;Version 11.0
* Removed: {{Code|hof:until}}
;Version 9.5
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu