Support negative step in xfrange to match frange - #442
Closed
uttam12331 wants to merge 1 commit into
Closed
Conversation
xfrange is documented as "Same as frange" and shares its argument handling, but its loop hard-coded the ascending condition `while cur < stop`, so a negative step yielded nothing. frange handles a negative step (its docstring pins `frange(5, 0, step=-1.25) == [5.0, 3.75, 2.5, 1.25]`), so `xfrange` with the same arguments silently returned an empty generator instead. Pick the loop condition based on the sign of step, and add a matching negative-step doctest.
Owner
|
Good catch, appreciate the effort. #443 reworked xfrange more broadly. Keep up the good work! |
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.
Summary
xfrangeis documented as "Same asfrange, but generator-based" and sharesfrange's exact argument handling (including therange()-style start/stop swap). But its loop hard-codes the ascending condition:so a negative step yields nothing.
frangesupports a negative step — its docstring pins it:whereas the equivalent
xfrangecall returned an empty generator:Fix
Choose the loop condition based on the sign of
step:Tests
Added a negative-step doctest to
xfrangemirroringfrange's:The existing positive-step doctest (
xfrange(1, 3, step=0.75)) is unchanged and still passes. I verifiedxfrangenow matchesfrangeacross all offrange's documented cases (ascending, empty, and the negative-step case).