From: "cassidify (Jakob Yanagibashi) via ruby-core" Date: 2026-11-14T13:53:25+00:00 Subject: [ruby-core:127490] [Ruby Feature#17016] Enumerable#accumulate Issue #17016 has been updated by cassidify (Jakob Yanagibashi). I think there is use case: `inject` returns only final accumulator, `accumulate` gives each successive accumulator. On top of cumulative sums and the event-sourcing use case described above, this can do running extrema, consecutive-failure counts, and state-machine transitions, and downstream method can filter results or stop early. The captured-variable workaround is reasonable, but behaves differently: ```ruby total = 0 totals = [1, 2, 3].lazy.map { |n| total += n } totals.first(2) # => [1, 3] totals.first(2) # => [4, 6] ``` Historical precedent alone does not justify another method, considering the maintenance costs. But this feels like a good fit alongside `map` and `inject`, not just special convenience. Therefore, I would support adding `accumulate` (with matching eager and lazy value semantics), and leave `inject` unchanged. I admit personally I'm also big fan of functional style, but giving a recurring operation a clear name and a well-defined behaviour seems worth it in general. ---------------------------------------- Feature #17016: Enumerable#accumulate https://bugs.ruby-lang.org/issues/17016#change-119958 * Author: parker (Parker Finch) * Status: Open ---------------------------------------- ## Proposal UPDATE: Changed proposed method name from `#scan_left` to `#accumulate`. Add an `#accumulate` method to `Enumerable`. ## Background `#accumulate` is similar to `#inject`, but it accumulates the partial results that are computed. As a comparison: ``` [1, 2, 3].inject(0, &:+) => 6 [1, 2, 3].accumulate(0, &:+) => [0, 1, 3, 6] ``` Notably, the `accumulate` operation can be done lazily since it doesn't require processing the entire collection before computing a value. I recently described `#accumulate`, and its relationship to `#inject`, more thoroughly in [this blog post](https://medium.com/building-panorama-education/scan-left-a-lazy-incremental-alternative-to-inject-f6e946f74c00). ## Reasoning We heavily rely on the accumulate operation. We use an [event-sourcing](https://martinfowler.com/eaaDev/EventSourcing.html) pattern, which means that we are scanning over individual "events" and building up the corresponding state. We rely on the history of states and need to do this lazily (we stream events because they cannot fit in memory). Thus the scan operation is much more applicable than the inject operation. We suspect that there are many applications that could leverage the scan operation. [This question](https://stackoverflow.com/questions/1475808/cumulative-array-sum-in-ruby) would be more easily answered by `#accumulate`. It is a natural fit for any application that needs to store the incrementally-computed values of an `#inject`, and a requirement for an application that needs to use `#inject` while maintaining laziness. ## Implementation There is a Ruby implementation of this functionality [here](https://github.com/panorama-ed/scan_left/) and an implementation in C [here](https://github.com/ruby/ruby/pull/3078). Update: @nobu has provided an alternative implementation [here](https://github.com/ruby/ruby/pull/1972). ## Counterarguments Introducing a new public method is committing to maintenance going forward and expands the size of the Ruby codebase -- it should not be done lightly. I think that providing the functionality here is worth the tradeoff, but I understand any hesitation to add yet more to Ruby! ---Files-------------------------------- scan_left_example.rb (2.93 KB) -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/