-
Notifications
You must be signed in to change notification settings - Fork 321
Feature request: a running fold adaptor #798
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
Comments
Running totals and That being said, I don't know if we would add a version of this:
If we were to try to implement this, I would suggest to benchmark it to see if it's faster or slower than current possibilities. |
Note that the scan version is more the analog of reduce, so will not "yield" the initial value: The function I wrote will produce an iterator of length n + 1 if This is part of why I think it's a good candidate for addition to itertools: one "obvious" way you might want to write it seems tricky at best to get right. An example where the types are not
Absolutely not attached to the name.
I would expect to call it as |
The name "accumulate" seems a reasonable name to me. I agree that we should be able to "accumulate" without If it produces n + 1 elements then we should not implement
EDIT: Working on a draft. EDIT 2: https://github.com/Philippe-Cholet/itertools/tree/accumulate-draft |
It's often useful to keep track of the intermediate results in a fold, as in Haskell's
scanl
, for example if you want to turn a iterator of numbers into an iterator of running totals. At first this looks like a job forscan
, but it was easier to write usingsuccessors
, as follows:Then
fold_map(1..4, 0, |x, y| x + y).collect_vec()
produces[0, 1, 3, 6]
, see playground link.Could a version of this be added to itertools?
The text was updated successfully, but these errors were encountered: