Skip to content

py: Fix range to support negative step #42

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

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
py: Fix range to support negative step
  • Loading branch information
corona10 committed Dec 27, 2018
commit d356b204da7085a8e1c47b064b0c41b582f56531
6 changes: 5 additions & 1 deletion py/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ func (it *RangeIterator) M__iter__() (Object, error) {
// Range iterator next
func (it *RangeIterator) M__next__() (Object, error) {
r := it.Index
if r >= it.Stop {
if it.Step >= 0 && r >= it.Stop {
return nil, StopIteration
}

if it.Step < 0 && r <= it.Stop {
return nil, StopIteration
}
it.Index += it.Step
Expand Down
5 changes: 5 additions & 0 deletions py/tests/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
b = [e for e in a]
assert len(a) == len(b)

a = range(100, 0, -1)
b = [e for e in a]
assert len(a) == 100
assert len(b) == 100

doc="finished"