Skip to content

Conversation

@ebonnal
Copy link
Owner

@ebonnal ebonnal commented Mar 12, 2025

Co-author: @pierrenodet

Add the behavior of itertools.accumulate (aka .scan in Scala/Haskell).

Examples

from streamable import running

cumulative_sum = (
    Stream(range(10))
    .map(running(lambda cumsum, i: cumsum + i, initial=0))
)

assert list(cumulative_sum) == [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

fibonacci = (
    Stream(range(10))
    .map(running(lambda acc, _: (acc[1], acc[0] + acc[1]), initial=(0, 1)))
    .map(itemgetter(0))
)

assert list(fibonacci) == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

TODO:

  • validate that this is the most elegant way to add this behavior
  • validate that the signature follows conventions existing elsewhere
  • more unit tests (e.g. are we happy with how it behaves in case exceptions are thrown by the binary func?)
  • find a concise yet explicit description for the README and the docstring (we can get inspiration from the itertools.accumulate's doc)
  • validate the choice of the example in the README

Co-authored-by: pierrenodet <[email protected]>
@codecov
Copy link

codecov bot commented Mar 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b172b49) to head (1424ca2).
Report is 26 commits behind head on main.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ebonnal ebonnal mentioned this pull request Mar 12, 2025
@ebonnal ebonnal force-pushed the 1.5.0-rc3 branch 5 times, most recently from d123207 to 2f11154 Compare March 12, 2025 22:47
@ebonnal ebonnal deleted the branch main March 13, 2025 23:13
@ebonnal ebonnal closed this Mar 13, 2025
@ebonnal ebonnal deleted the feat/running branch March 13, 2025 23:13
@ebonnal ebonnal restored the feat/running branch March 13, 2025 23:13
@ebonnal ebonnal reopened this Apr 7, 2025
@ebonnal ebonnal changed the base branch from 1.5.0-rc3 to main April 7, 2025 13:09
@ebonnal ebonnal force-pushed the main branch 6 times, most recently from 7db03a7 to 25aa525 Compare April 12, 2025 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants