Repository navigation
Range#each: iterate an endless range lazily instead of materialising it - #457
Merged
Merged
Conversation
Range#each built the whole element list with rangeElems, which raised "can't iterate from <begin>" for an endless range (1.., "a"..) because such a range has no finite element list. It now iterates an endless range lazily, the way Range#step already does: a String begin walks by #succ, an Integer or Bignum begin counts up with exact big.Int arithmetic (so an endless Bignum range does not degrade to Float and stall), and the block breaks out of the otherwise infinite walk. A begin with no successor (e.g. a Float) still raises a TypeError. Because every Enumerable method routes through #each, this also makes each_with_index, select, map, lazy, first(n), etc. work on an endless range. Bounded ranges keep their existing materialising path. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/range/each_spec 5 -> 7. (It also unblocks the endless-source example of Enumerator.product once that lands.) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Range#eachbuilt the whole element list withrangeElems, which raisedcan't iterate from <begin>for an endless range (1..,"a"..) because such arange has no finite element list. It now iterates an endless range lazily,
the way
Range#stepalready does: a String begin walks by#succ, an Integer orBignum begin counts up with exact
big.Intarithmetic (so an endless Bignumrange does not degrade to Float and stall), and the block breaks out of the
otherwise infinite walk. A begin with no successor (e.g. a Float) still raises a
TypeError.Because every Enumerable method routes through
#each, this also makeseach_with_index,select,map,lazy,first(n), etc. work on an endlessrange. Bounded ranges keep their existing materialising path.
Verified against ruby 4.0.6. Full package coverage stays at 100.0%.
Zero-regression rubyspec sweep;
core/range/each_spec5 → 7. (It alsounblocks the endless-source example of
Enumerator.productonce that lands.)🤖 Generated with Claude Code