Skip to content

Commit

Permalink
fn:while. qt4cg#80
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGruen committed Oct 13, 2022
1 parent 318c353 commit 04ea1b5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions specifications/xpath-functions-40/src/function-catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16579,6 +16579,86 @@ declare function fn:fold-right(
</fos:example>
</fos:examples>
</fos:function>
<fos:function name="while" prefix="fn">
<fos:signatures>
<fos:proto name="while" return-type="item()*">
<fos:arg name="input" type="item()*"/>
<fos:arg name="predicate" type="function(item()*) as xs:boolean"/>
<fos:arg name="action" type="function(item()*) as item()*"/>
</fos:proto>
</fos:signatures>
<fos:properties>
<fos:property>deterministic</fos:property>
<fos:property>context-independent</fos:property>
<fos:property>focus-independent</fos:property>
<fos:property>higher-order</fos:property>
</fos:properties>
<fos:summary>
<p>Applies the predicate function to the supplied input. In case of success, the input
is returned. Otherwise, the input is passed on to the action function, and the result is
applied to the predicate function and returned or taken as new input.</p>
</fos:summary>
<fos:rules>
<p>The effect of the function is equivalent to the following implementation in XQuery:</p>
<eg><![CDATA[
declare function fn:while(
$input as item()*,
$predicate as function(item()*) as xs:boolean,
$action as function(item()*) as item()*
) as item()* {
if ($predicate($input)) then (
fn:while($action($input), $predicate, $action)
) else (
$input
)
};]]></eg>
</fos:rules>
<fos:errors>
<p>As a consequence of the function signature and the function calling rules, a type error
occurs if the supplied predicate function cannot be applied to a single argument, or if
if returns anything other than a single <code>xs:boolean</code> item; there is no
conversion to an effective boolean value. Alternatively, an error occurs if the supplied
action function cannot be applied to a single argument.</p>
</fos:errors>
<fos:examples>
<fos:example>
<fos:test>
<fos:expression><![CDATA[fn:while(2, -> { . < 100 }, -> { . * . })]]></fos:expression>
<fos:result>256</fos:result>
</fos:test>
</fos:example>
<fos:example>
<fos:test>
<fos:expression><![CDATA[fn:while(
1 to 9,
function($seq) { head($seq) < 5 },
function($seq) { tail($seq) }
)]]></fos:expression>
<fos:result>(5, 6, 7, 8, 9)</fos:result>
</fos:test>
</fos:example>
<fos:example>
<fos:test>
<fos:expression><![CDATA[let $input := 3936256
return fn:while(
$input,
function($result) { abs($result * $result - $input) >= 0.0000000001 },
function($guess) { ($guess + $input div $guess) div 2 }
)]]></fos:expression>
<fos:result>1984</fos:result>
<fos:postamble>This computes the square root of a number.</fos:postamble>
</fos:test>
</fos:example>
<fos:example>
<fos:test>
<fos:expression><![CDATA[let $input := (0 to 4, 6 to 10)
return fn:while(0, function($n) { $n = $input }, function($n) { $n + 1 })]]></fos:expression>
<fos:result>5</fos:result>
<fos:postamble>This returns the first positive number missing in a sequence.</fos:postamble>
</fos:test>
</fos:example>
</fos:examples>
</fos:function>
<fos:function name="for-each-pair" prefix="fn">
<fos:signatures>
<fos:proto name="for-each-pair" return-type="item()*">
Expand Down
3 changes: 3 additions & 0 deletions specifications/xpath-functions-40/src/xpath-functions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5888,6 +5888,9 @@ correctly in all browsers, depending on the system configuration.</emph></p>-->
<div3 id="func-fold-right">
<head><?function fn:fold-right?></head>
</div3>
<div3 id="func-while">
<head><?function fn:while?></head>
</div3>
<div3 id="func-for-each-pair">
<head><?function fn:for-each-pair?></head>
</div3>
Expand Down

0 comments on commit 04ea1b5

Please sign in to comment.