-
Notifications
You must be signed in to change notification settings - Fork 18
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
1341 Drop $position callback from many functions #1735
base: master
Are you sure you want to change the base?
Conversation
To be honest, I am not very thrilled to possibly see this feature rolled back. Apart from my strong personal preference for keeping As for for $item at $pos in $data
return $pos || '. ' || $item
(: vs. :)
for-each($data, fn($item, $pos) { $pos || '. ' || $item })
(: vs. :)
for-each(
numbered-items($data),
fn() { ?position || '. ' || ?item }
) Next, the first two variants give you type safety: A mistyped Finally, if we really go that path, I think we should remove |
If I remember correctly, most of the discussion about positional arguments was about |
309ce2a
to
81620d1
Compare
81620d1
to
9ec0408
Compare
I compared the current specification of One observation is that the last example for fn:fold-left in the current PR has not been updated from the current version and still calls a version of fn:fold-left, whose $action argument is a function accepting 3 arguments, the last one $pos: let $input := (11 to 21, 21 to 31)
let $target := 21
return fold-left($input, (),
fn($result, $curr, $pos) {
$result, if ($curr = $target) { $pos }
}
) This probably needs to be: let $input := (11 to 21, 21 to 31)
let $target := 21
return fold-left(numbered-items($input), (),
fn($result, $curr) {
$result, if ($curr?item = $target) {$curr?position }
}
) Here are screen-dumps of the corresponding "Formal Equivalent" sections so that one can see the huge complexity of the current one and how much more elegant and concise is the one in this PR: Compare to this: |
Responding to the discussion in #1341, this (somewhat experimental) PR explores the possibility of dropping the optional
$position argument to the callback of many higher-order functions such as some(), every(), filter(), for-each(), fold-left(), fold-right(). Instead, it provides the option to wrap the input sequence in a call of numbered-items() which replaces each item in the input with an (item, position) pair.
I've done this only (so far) for higher-order sequence functions, but the intent is that the same could be done for arrays and (potentially) maps.
I left the position argument in place for a few functions where losing it seemed to cause genuine inconvenience:
The main benefit is that we provide one basic mechanism which is automatically available everywhere, which means we don't have to have debates about whether or not there is a use case for adding position information to (say) fold-left or scan-right.
A further benefit is that the functions defined for sequences automatically become available for arrays and maps. I haven't yet explored the impact on maps and arrays; I will wait first to see what the reaction is to this proposal.