-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 Enumerable#each_cons_pair and Iterator#cons_pair yielding a tuple #8332
Add Enumerable#each_cons_pair and Iterator#cons_pair yielding a tuple #8332
Conversation
I like this! What if we name it Though it's true that one of the most common cases for |
I don't see a reason for adding a number suffix. Iterating over pairs is by far the most common use case. If we wanted to add others, there's nothing hindering us from adding |
Want to rebase for a merge commit? |
5510382
to
fd01717
Compare
I love this optimization but I feel like the chosen size |
I like @vlazar's suggestion. Otherwise, the expected block arguments differ based on the presence or not of the argument. I like the explicitness of |
I guess that might be a good enhancement, but waiting for feedback on this suggestion. |
I think |
+1 from me, might want to go ahead with this |
|
fd01717
to
061e4a4
Compare
Renamed to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge upon renaming the PR for the changelog.
Enumerable#each_cons
has a variable chunk size that can be selected dynamically.This PR adds a special case for the most common use case of iterating over pairs. It simply yields tuples of adjacent items. This avoids heap allocations and makes it easier to use because you don't need to deconstruct an array.
Benchmark:
It's a convenience feature that also improves performance.
I've added the same behaviour to
Iterator#cons