Skip to content
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

Add methods for cumulative folding and prefix sums #10789

Merged

Conversation

HertzDevil
Copy link
Contributor

Closes #8496.

The interfaces (Enumerable#accumulate, Iterator#accumulate, Iterable#each_accumulated) are based on #8496 (comment), i.e. block-less overloads are used for prefix sums. All overloads are O(n).

src/iterable.cr Outdated Show resolved Hide resolved
@@ -44,6 +44,92 @@ describe Iterator do
end
end

describe "#accumulate" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use the new Iterator spec helpers here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it can be done in a separate PR

# the collection. This initial value is always present in the returned array.
#
# ```
# [1, 3, 5, 7].accumulate(9) { |x, y| x * y } # => [9, 9, 27, 135, 945]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a different value than 1 here? It might make the behavior easier to understand

@asterite asterite added this to the 1.1.0 milestone Jun 18, 2021
@asterite asterite merged commit df2cbee into crystal-lang:master Jun 18, 2021
@HertzDevil HertzDevil deleted the feature/enumerable-accumulate branch June 18, 2021 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement cumulative folding
6 participants