diff --git a/specifications/xpath-functions-40/src/function-catalog.xml b/specifications/xpath-functions-40/src/function-catalog.xml index 2f0750fdc..ef8c07bfe 100644 --- a/specifications/xpath-functions-40/src/function-catalog.xml +++ b/specifications/xpath-functions-40/src/function-catalog.xml @@ -19236,7 +19236,7 @@ return map:keys($ann) || ': ' || string-join(map:values($ann), ', ') -

Applies the function item $action to every item from the sequence $input +

Applies the function item $action to every item from the sequence $input in turn, returning the concatenation of the resulting sequences in order.

@@ -19381,39 +19381,40 @@ return filter( repeatedly to each item in turn, together with an accumulated result value.

-

The function is equivalent to the following implementation in XQuery:

+

When the supplied $action has an arity of two or less, + the function is equivalent to the following implementation in XQuery:

+ +

The case where $action has an arity of three can be handled by first building a sequence + of (position, item) pairs, and then calling the arity-2 function on this sequence:

+ + + let $pairs := $input ! {'position': position(), 'item': .} + return fold-left2($pairs, $zero, fn($zero, $pair) { + $action($zero, $pair?item, $pair?position) + } +}]]>

As a consequence of the function signature and the function calling rules, a type error - occurs if the supplied function $action cannot be applied to two arguments, where - the first argument is either the value of $zero or the result of a previous - application of $action, and the second - is any single item from the sequence $input.

+ occurs if the supplied function $action cannot be applied to the supplied arguments.

This operation is often referred to in the functional programming literature as @@ -19424,8 +19425,7 @@ declare function fold-left( value (such as zero in the case of addition, one in the case of multiplication, or a zero-length string in the case of string concatenation) that causes the function to return the value of the other argument unchanged.

-

The value of the third argument of $action corresponds to the position - of the item in the input sequence. It is initally set to 1.

+
@@ -19436,7 +19436,8 @@ declare function fold-left( fn($a, $b) { $a + $b } ) 15 - This returns the sum of the items in the sequence + This returns the sum of the items in the sequence. The result is computed + as ((((0+1)+2)+3)+4)+5). @@ -19545,8 +19546,7 @@ return fold-left($input, (), deterministic context-independent - focus-independent - + focus-independent special-streaming-rules @@ -19554,37 +19554,41 @@ return fold-left($input, (), repeatedly to each item in turn, together with an accumulated result value.

-

The function is equivalent to the following implementation in XQuery:

+

When the supplied $action has an arity of two or less, + the function is equivalent to the following implementation in XQuery:

+ +

The case where $action has an arity of three can be handled by first building a sequence + of (position, item) pairs, and then calling the arity-2 function on this sequence:

+ + + let $pairs := $input ! {'position': position(), 'item': .} + return fold-right2($pairs, $zero, fn($zero, $pair) { + $action($zero, $pair?item, $pair?position) + } +}]]> +

As a consequence of the function signature and the function calling rules, a type error - occurs if the supplied function $action cannot be applied to two arguments, where - the first argument is any item in the sequence $input, and the second is either - the value of $zero or the result of a previous application of - $action.

+ occurs if the supplied function $action cannot be applied to the supplied arguments.

@@ -19599,9 +19603,6 @@ declare function fold-right(

In cases where the function performs an associative operation on its two arguments (such as addition or multiplication), fn:fold-right produces the same result as fn:fold-left.

-

The value of the third argument of $action corresponds to the position - of the item in the input sequence. Thus, in contrast to fn:fold-left, - it is initally set to the number of items in the input sequence.

@@ -19612,7 +19613,8 @@ declare function fold-right( fn($a, $b) { $a + $b } ) 15 - This returns the sum of the items in the sequence + This returns the sum of the items in the sequence. + The result is computed as (0+(1+(2+(3+(4+5))))). @@ -21521,7 +21523,7 @@ return fold-left($MAPS, {}, of the corresponding values, retaining their order in the input sequence.

The effect of the function is equivalent to the expression:

- map:pairs($week) => map:build(fn { ?key }, fn { ?value }, $combine) + map:build($input, fn { ?key }, fn { ?value }, $combine)
@@ -27017,7 +27019,8 @@ return array:filter( array.

-

The function is equivalent to the following expression:

+

The function is equivalent to the following expression, which converts the array to a sequence and then + invokes fn:fold-left:

-

The function is equivalent to the following expression:

+

The function is equivalent to the following expression, which converts the array to a sequence and then + invokes fn:fold-right:

file: scheme.

- + @@ -31344,51 +31348,63 @@ path with an explicit file: scheme.

focus-independent
-

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.

+

Produces a sequence containing intermediate results of an evaluation of fn:fold-left.

-

The function is equivalent to the following implementation in XPath (return clause added in comments for completeness):

+

In the case where the supplied $action has an arity of two or less, + the function is equivalent to the following implementation in XQuery:

+ +) as item()* { + array{$zero}, + if (exists($input)) { + scan-left2( + tail($input), + $action($zero, head($input)), + $action + ) + } +};]]>
+ +

The case where $action has an arity of three can be handled by first building a sequence + of (position, item) pairs, and then calling the arity-2 function on this sequence:

+ + + +

The result is a sequence of arrays representing intermediate results. The first array + holds the value of $zero. Subsequent arrays, one per item in $input, + represent the result of applying fn:fold-left (with the same values for + $zero and $action) to the subsequence of $input + ending at that item.

+ +

As a consequence of the function signature and the function calling rules, a type error - occurs if the supplied function $action cannot be applied to two arguments, where - the first argument is either the value of $zero or the result of a previous - application of $action, and the second - is any single item from the sequence $input.

+ occurs if the supplied function $action cannot be applied to the supplied arguments.

- - -

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. -

-
-
+

The number of arrays + in the result is the same as the number of items in $input plus one.

+

The fact that the function has the same signature as fn:fold-left + means that this function can conveniently be used to study the behavior of + a call on fn:fold-left with the same arguments, perhaps for + diagnostic purposes. It can also be used to produce the running totals of + a computation.

@@ -31399,31 +31415,25 @@ let $scan-left := fn( - scan-left(1 to 3, 0, op('-')) - [ 0 ], [ -1 ], [ -3 ], [ -6 ] + scan-left(1 to 5, 0, op('-')) + [ 0 ], [ -1 ], [ -3 ], [ -6 ], [ -10 ], [ -15 ] - + -

Produce the intermediate results of mapping each number in a sequence to its doubled value. - This example shows the necessity to place each intermediate result (sequence) into a singleton array - otherwise - the sequence of sequences (intermediate results) would not be possible to express as a single sequence - without losing completely the intermediate results.

- let $double := fn($x) { 2 * $x } -return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) }) - [ () ], [ 2 ], [ (2, 4) ], [ (2, 4, 6) ] ] + scan-left(1 to 5, 1, op('*')) + [ 1 ], [ 1 ], [ 2 ], [ 6 ], [ 24 ], [ 120 ] -
+ -

Produce the factorials of all numbers from 0 to 5

- scan-left(1 to 5, 1, op('*')) - [ 1 ], [ 1 ], [ 2 ], [ 6 ], [ 24 ], [ 120 ] + scan-left(1 to 3, (), fn($seq, $it) { $seq , fn(.*2) }) + [ ], [ 2 ], [ 2, 4 ], [ 2, 4, 6 ] -
+
- Proposed for 4.0 + New in 4.0 @@ -31432,7 +31442,7 @@ return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) })
- + @@ -31441,51 +31451,60 @@ return scan-left(1 to 3, (), fn($seq, $it) { $seq , $double($it) })focus-independent -

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.

+

Produces a sequence containing intermediate results of an evaluation of fn:fold-right.

-

The function is equivalent to the following implementation in XPath (return clause in comments added for completeness):

+

In the case where the supplied $action has an arity of two or less, + the function is equivalent to the following implementation in XQuery:

+ +}]]> + +

The case where $action has an arity of three can be handled by first building a sequence + of (position, item) pairs, and then calling the arity-2 function on this sequence:

+ + + +

The result is a sequence of arrays representing intermediate results. The last array + holds the value of $zero. Previous arrays, one per item in $input, + represent the result of applying fn:fold-right (with the same values for + $zero and $action) to the subsequence of $input + starting at that item.

As a consequence of the function signature and the function calling rules, a type error - occurs if the supplied function $action cannot be applied to two arguments, where - the first argument is any item in the sequence $input, and the second is either - the value of $zero or the result of a previous application of - $action.

+ occurs if the supplied function $action cannot be applied to the supplied arguments.

- - -

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. -

-
-
-
+

The number of arrays + in the result is the same as the number of items in $input plus one.

+

The fact that the function has the same signature as fn:fold-right + means that this function can conveniently be used to study the behavior of + a call on fn:fold-right with the same arguments, perhaps for + diagnostic purposes. It can also be used to produce the running totals of + a computation.

+ @@ -31502,7 +31521,7 @@ let $scan-right := function( - Proposed for 4.0 + New in 4.0