Skip to content

Commit

Permalink
Merge pull request #1174 from ChristianGruen/1173
Browse files Browse the repository at this point in the history
1173 array:build, map:build: Positional access
  • Loading branch information
ndw authored Apr 30, 2024
2 parents 2ee26b8 + fc109e5 commit b8412e6
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions specifications/xpath-functions-40/src/function-catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22181,8 +22181,8 @@ else map:put($MAP, $KEY, $ACTION(())</eg>
<fos:signatures>
<fos:proto name="build" return-type="map(*)">
<fos:arg name="input" type="item()*"/>
<fos:arg name="keys" type="function(item()) as xs:anyAtomicType*" default="fn:identity#1"/>
<fos:arg name="value" type="function(item()) as item()*" default="fn:identity#1"/>
<fos:arg name="keys" type="function(item(), xs:integer) as xs:anyAtomicType*" default="fn:identity#1"/>
<fos:arg name="value" type="function(item(), xs:integer) as item()*" default="fn:identity#1"/>
<fos:arg name="combine" type="function(item()*, item()*) as item()*" default="fn:op(',')"/>
</fos:proto>
</fos:signatures>
Expand All @@ -22209,9 +22209,9 @@ else map:put($MAP, $KEY, $ACTION(())</eg>
</ulist>
<p>More formally, the function returns the value of the expression:</p>
<eg>
fold-left($input, {}, fn($map, $item) {
let $v := $value($item)
return fold-left($key($item), $map, fn($m, $k) {
fold-left($input, {}, fn($map, $item, $pos) {
let $v := $value($item, $pos)
return fold-left($key($item, $pos), $map, fn($m, $k) {
if (map:contains($m, $k)) then (
map:put($m, $k, $combine($m($k), $v))
) else (
Expand Down Expand Up @@ -22287,6 +22287,16 @@ fold-left($input, {}, fn($map, $item) {
<fos:postamble>Constructs a map where the key is the first character of an input item, and where the corresponding value
is the total string-length of the items starting with that character.</fos:postamble>
</fos:test>
<fos:test>
<fos:expression><eg>map:build(
('Wang', 'Liu', 'Zhao'),
keys := fn($name, $pos) { $name },
value := fn($name, $pos) { $pos }
)</eg></fos:expression>
<fos:result>{ "Wang": 1, "Liu": 2, "Zhao": 3 }</fos:result>
<fos:postamble>Returns an inverted index for the input sequence with the
string stored as key and the position stored as value.</fos:postamble>
</fos:test>
<fos:test>
<fos:expression><eg><![CDATA[
let $titles := <titles>
Expand All @@ -22308,7 +22318,6 @@ return map:build($titles/title, fn($title) { $title/ix })
)
}]]></eg></fos:result>
</fos:test>

</fos:example>
<fos:example>
<p>The following expression creates a map whose keys are employee <code>@ssn</code> values, and whose
Expand Down Expand Up @@ -26760,7 +26769,7 @@ array:for-each-pair(
<fos:signatures>
<fos:proto name="build" return-type="array(*)">
<fos:arg name="input" type="item()*" usage="inspection"/>
<fos:arg name="action" type="function(item()) as item()*" usage="inspection" default="fn:identity#1"/>
<fos:arg name="action" type="function(item(), xs:integer) as item()*" usage="inspection" default="fn:identity#1"/>
</fos:proto>
</fos:signatures>
<fos:properties>
Expand All @@ -26778,7 +26787,12 @@ array:for-each-pair(
<p>Informally, <code>array:build#2</code> applies the supplied function to each item
in the input sequence, and the resulting sequence becomes one member of the returned array.</p>
<p>More formally, <code>array:build#2</code> returns the result of the expression:</p>
<eg>array:of-members($input ! { 'value': $action(.) })</eg>
<eg>
array:of-members(
for $item at $pos in $input
return { 'value': $action($item, $pos) }
)
</eg>
</fos:rules>
<fos:notes>
<p>The single-argument function <code>array:build($input)</code> is equivalent to the XPath
Expand Down Expand Up @@ -26808,6 +26822,17 @@ array:for-each-pair(
<fos:expression><eg>array:build(1 to 5, fn { array { 1 to . } })</eg></fos:expression>
<fos:result>[ [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 2, 3, 4 ], [ 1, 2, 3, 4, 5 ] ]</fos:result>
</fos:test>
<fos:test>
<fos:expression><eg>
array:build(
(0x41 to 0x48) ! char(.),
fn($char, $pos) {
if($pos mod 2 = 0) then lower-case($char) else $char
}
)
</eg></fos:expression>
<fos:result>[ "A", "b", "C", "d", "E", "f", "G", "h" ]</fos:result>
</fos:test>
</fos:example>
</fos:examples>
<fos:history>
Expand Down

0 comments on commit b8412e6

Please sign in to comment.