Skip to content

range functions should iterate backwards as well #1817

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

Closed
brson opened this issue Feb 12, 2012 · 12 comments
Closed

range functions should iterate backwards as well #1817

brson opened this issue Feb 12, 2012 · 12 comments
Assignees
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@brson
Copy link
Contributor

brson commented Feb 12, 2012

Right now we have a bunch of range functions in mod int, etc that all iterate from low to high. It would be convenient if they would detect that the arguments were in high to low order and provide a reverse range. If not, then we need rrange functions.

@andreisavu
Copy link

Is this what you are thinking about?

#[doc = "Iterate over the range [`begin`..`end`)"]
fn range(begin: i16, end: i16, it: fn(i16)) {
    let i = begin;
    if begin < end {
        while i < end { it(i); i += 1i16; }
    } else {
        while i > end { it(i); i -= 1i16; }
    }
}

Is there a test suite for libcore?

@brson
Copy link
Contributor Author

brson commented Feb 12, 2012

Yes, that's what I was thinking. The core tests are located in the core crate, near the code they test (marked with the #[test] attribute).

@brson
Copy link
Contributor Author

brson commented Feb 12, 2012

Although maybe it makes sense for the reverse range to cover the same numbers, so it would still include the low value, but not the high value. Not sure. I wonder if there's a precedent in another language.

@andreisavu
Copy link

Would it be a better idea to have an implementation like in Python?

range([start,] stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.

@brson
Copy link
Contributor Author

brson commented Feb 12, 2012

Python's range function does it the way you wrote if you give it a -1 step value, so I guess that makes sense. If you provide a low value that is greater than the high value to python's range it returns an empty list. Maybe there's a good reason for that and we should have a range_step function or somesuch that requires you to be explicit. I'd be interested in hearing other opinions.

@brson
Copy link
Contributor Author

brson commented Feb 12, 2012

I would be fine with copying python.

@andreisavu
Copy link

Ok. I will attach a pull request later today.

@jruderman
Copy link
Contributor

I think a separate rrange function would be safer. One programmer’s reverse range is another’s base case empty range.

@catamorphism
Copy link
Contributor

@andreisavu -- are you still working on this?

@ghost ghost assigned brson Apr 12, 2012
@andreisavu
Copy link

No, I'm busy with other things.
On Apr 13, 2012 2:30 AM, "Tim Chevalier" <
[email protected]>
wrote:

@andreisavu -- are you still working on this?


Reply to this email directly or view it on GitHub:
#1817 (comment)

@catamorphism
Copy link
Contributor

Ok, then this is up for grabs.

@catamorphism
Copy link
Contributor

Fixed as of 982cf90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants