-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(array|map):replace → *:substitute or *:change #583
Comments
Do we really need the new functions? If position arguments are added to for-each($sequence, fn($item, $pos) {
if($pos > 3) { $item }
})
array:for-each($array, fn($member, $pos) {
if($pos = (1, 2, 3, 7) or $pos > 10) then $member else $member + 1
}) For maps, either |
See also issue #553. |
Similar to Related: #104 |
This concern has just been reported back to us by an early adopter, so I would be in favor of dropping Regarding substitutions, |
My current instinct is to drop map:replace and array:replace in favour of the syntax proposed in PR #832
It has been remarked that we are providing too many solutions to every problem we identify. |
We need to revisit this question when we've done the update syntax. |
We have no tests for |
#1609 proposes revisions for |
Some observations:
$positions as xs:integer*
to array:for-each. However, that could be confusing: people might imagine that the items at positions not present in the list are discarded, rather than being returned unchanged in the result. So I propose we don't do that.fn:replace
does something completely different.map:substitute
(but it's not quite the same, because it processes every entry in the map).In the interests of alignment, I propose we have three functions:
fn:substitute($input as item()*, $positions as xs:positiveInteger*, $action as function(item()) as item())
equivalent to
for $it at $pos in $item return if ($pos = $positions) then $action($it) else $it
array:substitute($array as array(*), $positions as xs:positiveInteger*, $action as function(item()*) as item()*)
equivalent to
array{for member $it at $pos in $item return if ($pos = $positions) then $action($it) else $it}
map:substitute($map as map(*), $keys as xs:anyAtomicValue*, $action as function(anyAtomicValue, item()*) as item()*)
For the first two functions, we don't really need to allow the second argument to be omitted, because it would then be equivalent to the corresponding for-each() function. Unfortunately that's not quite true of map:for-each(), because it doesn't return a map. However if you want to do a functional replacement of every entry in a map, it can be done easily enough with
map:build(map:key-value-pairs($map), function{?key}, function{$action(?value)})
so we're not really losing anything.
The text was updated successfully, but these errors were encountered: