-
Notifications
You must be signed in to change notification settings - Fork 15
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
fn:scan-left and fn:scan-right - produce accumulation of results #948
Comments
Looks like these could be defined in terms of the existing fold functions? |
With a scan, the intermediate results are returned as well. While it’s possible to generate a sequence with the intermediate results with fold, it’s more convenient to have a custom function for it. I know that Dimitre will disagree, but as hardly anyone uses |
Thanks for replying. Agree about fold-right! And in the XSLT 3 course i run, some people have said they'd struggled for ages with fold-left() before having an example and working through it. But i am not sure that they would find scan() any easier, and having two functions to learn instead of one is a burden sometimes. I’d vote for less-is-more on this one. |
Yes, learning folds may take a while. Until today, I don’t understand how our spec tries to explain how they work (and I wish we would revise the text; related: #864 (comment)). Instead, I learned folding by looking at other languages (or books ;). My feeling is that once you’ve understood folds, scans are easy. Maybe we can find a better name, though. |
I have certainly found the need for something equivalent to scan-left. I agree that it's very difficult to explain these functions to users (and that's especially true for fold-right, which I still struggle with myself) and we should certainly try to do a better job on explanation (especially by example), but I don't think that's an absolute reason for leaving them out. |
Here is more about the use-cases for the scan functions: https://www.reddit.com/r/haskell/comments/s737lq/scan/ And especially here: https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf |
As there seem to be no more comments, I will proceed to creating a PR. Any objections? |
PRs are always helpful. |
Submitted the PR, can be viewed here now: #957 |
fn:scan-left and fn:scan-right - produce accumulation of results
In XPath 4.0 so far we still don't have a convenient way to express the functionality of producing a series of accumulated (accrued) results when applying a folding function over a collection (sequence, array, ...) of items. The general use-case for this is the task to produce a sequence of running totals when applying an operation over a sequence of data points: produce the partial sums of loan payments over fixed periods, produce the compounded amounts of a deposit with fixed interest rate over years, ..., etc.
Two functions (shamelessly borrowed from Haskell):
fn:scan-left
This function has a similar signature to that of
fn:fold-left
and produces the same final result, however it produces the complete (ordered) sequence of all partial results from every new value the accumulator gets during the evaluation offn:fold-left
.Signature
Properties
This function is ·deterministic·, ·context-independent·, and ·focus-independent·.
Rules
The function is equivalent to the following implementation in XPath(return clause added for completeness):
Examples:
produces:
fn:scan-right
This function has a similar signature to that of
fn:fold-right
and produces the same final result, however it produces the complete (ordered) sequence of all partial results from every new value the accumulator gets during the evaluation offn:fold-right
.Signature
Properties
This function is ·deterministic·, ·context-independent·, and ·focus-independent·.
Rules
The function is equivalent to the following implementation in XPath(return clause added for completeness):
Examples:
produces:
The text was updated successfully, but these errors were encountered: