Skip to content

Commit

Permalink
Rewrite of scan-left and scan-right
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhkay committed Jun 23, 2024
1 parent 8f3697a commit 1209589
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions specifications/xpath-functions-40/src/function-catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31345,7 +31345,7 @@ path with an explicit <code>file:</code> scheme.</p>
<fos:proto name="scan-left" return-type="array(*)*">
<fos:arg name="input" type="item()*" usage ="navigation"/>
<fos:arg name="zero" type="item()*"/>
<fos:arg name="action" type="fn(item()*, item()) as item()*" usage="inspection"/>
<fos:arg name="action" type="fn(item()*, item(), xs:integer) as item()*" usage="inspection"/>
</fos:proto>
</fos:signatures>
<fos:properties>
Expand All @@ -31354,11 +31354,16 @@ path with an explicit <code>file:</code> scheme.</p>
<fos:property>focus-independent</fos:property>
</fos:properties>
<fos:summary>
<p>Produces the complete (ordered) sequence of all partial results from every new value
the accumulator is assigned to during the evaluation of fn:fold-left.</p>
<p>Produces the complete (ordered) sequence of all intermediate
results of an evaluation of <code>fn:fold-left</code>.</p>
</fos:summary>
<fos:rules>
<p>The function is equivalent to the following implementation in XPath (return clause added in comments for completeness):</p>
<p>The result of the function is the value of the expression:</p>
<eg>(1 to count($input)) !
array{ slice($input, end := .) => fold-left($zero, $action) }</eg>

<!--
in XPath (return clause added in comments for completeness):</p>
<eg><![CDATA[
let $scan-left-inner := fn(
$input as item()*,
Expand All @@ -31381,36 +31386,34 @@ let $scan-left := fn(
$scan-left-inner($input, $zero, $action, $scan-left-inner)
}
(: return $scan-left(1 to 10, 0, op('+')) :)
]]></eg>
]]></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 function <code>$action</code> cannot be applied to two arguments, where
the first argument is either the value of <code>$zero</code> or the result of a previous
application of <code>$action</code>, and the second
is any single item from the sequence <code>$input</code>.</p>
<p>See <code>fn:fold-left</code>: errors are raised in the same situations.</p>
</fos:errors>
<fos:notes>
<olist>
<item>
<p>Note that each intermediate result is placed in a separate singleton array.
This is necessary because we cannot represent a sequence of results, some or all of which are
a sequence - that is "sequence of sequences" as just a single sequence.
</p>
</item>
</olist>
<p>A practical implementation might be expected to evaluate the result
incrementally in a single pass of the input; the equivalent expression
given in the rules above is provided purely for formal specification
purposes.</p>
<p>Each intermediate result is placed in a separate array. The number of arrays
in the result is the same as the number of items in <code>$input</code>.</p>
<p>The fact that the function has the same signature as <code>fn:fold-left</code>
means that this function can conveniently be used to study the behavior of
an call on <code>fn:fold-left</code> with the same arguments, perhaps for
diagnostic purposes.</p>
</fos:notes>
<fos:examples>
<fos:example>
<fos:test>
<fos:expression><eg>scan-left(1 to 5, 0, op('+'))</eg></fos:expression>
<fos:result>[ 0 ], [ 1 ], [ 3 ], [ 6 ], [ 10 ], [ 15 ]</fos:result>
<fos:result>[ 1 ], [ 3 ], [ 6 ], [ 10 ], [ 15 ]</fos:result>
</fos:test>
</fos:example>
<fos:example>
<fos:test>
<fos:expression><eg>scan-left(1 to 3, 0, op('-'))</eg></fos:expression>
<fos:result>[ 0 ], [ -1 ], [ -3 ], [ -6 ]</fos:result>
<fos:result>[ -1 ], [ -3 ], [ -6 ]</fos:result>
</fos:test>
</fos:example>
<fos:example>
Expand All @@ -31428,7 +31431,7 @@ return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) })</eg></fos:ex
<p> Produce the factorials of all numbers from 0 to 5</p>
<fos:test>
<fos:expression><eg>scan-left(1 to 5, 1, op('*'))</eg></fos:expression>
<fos:result>[ 1 ], [ 1 ], [ 2 ], [ 6 ], [ 24 ], [ 120 ]</fos:result>
<fos:result>[ 1 ], [ 2 ], [ 6 ], [ 24 ], [ 120 ]</fos:result>
</fos:test>
</fos:example>
</fos:examples>
Expand All @@ -31442,7 +31445,7 @@ return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) })</eg></fos:ex
<fos:proto name="scan-right" return-type="array(*)*">
<fos:arg name="input" type="item()*" usage ="navigation"/>
<fos:arg name="zero" type="item()*"/>
<fos:arg name="action" type="fn(item()*, item()) as item()*" usage="inspection"/>
<fos:arg name="action" type="fn(item()*, item(), xs:integer) as item()*" usage="inspection"/>
</fos:proto>
</fos:signatures>
<fos:properties>
Expand All @@ -31451,11 +31454,15 @@ return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) })</eg></fos:ex
<fos:property>focus-independent</fos:property>
</fos:properties>
<fos:summary>
<p>Produces the complete (ordered) sequence of all partial results from every new value
the accumulator is assigned to during the evaluation of fn:fold-right.</p>
<p>Produces the complete (ordered) sequence of all intermediate
results of an evaluation of <code>fn:fold-right</code>.</p>
</fos:summary>
<fos:rules>
<p>The function is equivalent to the following implementation in XPath (return clause in comments added for completeness):</p>
<p>The result of the function is the value of the expression:</p>

<eg>reverse(1 to count($input)) !
array{ slice($input, start := .) => fold-right($zero, $action) }</eg>
<!--<p>The function is equivalent to the following implementation in XPath (return clause in comments added for completeness):</p>
<eg><![CDATA[
let $scan-right-inner := fn(
$input as item()*,
Expand All @@ -31477,37 +31484,34 @@ let $scan-right := function(
$scan-right-inner($input, $zero, $f, $scan-right-inner)
}
(: return $scan-right(1 to 10, 0, op('+')) :)
]]></eg>
]]></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 function <code>$action</code> cannot be applied to two arguments, where
the first argument is any item in the sequence <code>$input</code>, and the second is either
the value of <code>$zero</code> or the result of a previous application of
<code>$action</code>.</p>
<p>See <code>fn:fold-left</code>: errors are raised in the same situations.</p>
</fos:errors>
<fos:notes>
<olist>
<item>
<p>Note that each intermediate result is placed in a separate singleton array.
This is necessary because we cannot represent a sequence of results, some or all of which are
a sequence - that is "sequence of sequences" as just a single sequence.
</p>
</item>
</olist>
</fos:notes>
<p>A practical implementation might be expected to evaluate the result
incrementally in a single right-to-left pass of the input; the equivalent expression
given in the rules above is provided purely for formal specification
purposes.</p>
<p>Each intermediate result is placed in a separate array. The number of arrays
in the result is the same as the number of items in <code>$input</code>.</p>
<p>The fact that the function has the same signature as <code>fn:fold-right</code>
means that this function can conveniently be used to study the behavior of
an call on <code>fn:fold-right</code> with the same arguments, perhaps for
diagnostic purposes.</p>
</fos:notes>
<fos:examples>
<fos:example>
<fos:test>
<fos:expression><eg>scan-right(1 to 5, 0, op('+'))</eg></fos:expression>
<fos:result><eg>[ 55 ], [ 54 ], [ 52 ], [ 49 ], [ 45 ],
[ 40 ], [ 34 ], [ 27 ], [ 19 ], [ 10 ], [ 0 ]</eg></fos:result>
<fos:result><eg>[ 5 ], [ 9 ], [ 12 ], [ 14 ], [ 15 ]</eg></fos:result>
</fos:test>
</fos:example>
<fos:example>
<fos:test>
<fos:expression><eg>scan-right(1 to 3, 0, op('-'))</eg></fos:expression>
<fos:result>[ 2 ], [ -1 ], [ 3 ], [ 0 ]</fos:result>
<fos:expression><eg>scan-right(1 to 5, 0, op('-'))</eg></fos:expression>
<fos:result>[ 5 ], [ -1 ], [ 4 ], [ -2 ], [ 3 ]</fos:result>
</fos:test>
</fos:example>
</fos:examples>
Expand Down

0 comments on commit 1209589

Please sign in to comment.