Skip to content
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

iter module and ranges #2715

Closed
jesse99 opened this issue Jun 23, 2012 · 5 comments
Closed

iter module and ranges #2715

jesse99 opened this issue Jun 23, 2012 · 5 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@jesse99
Copy link
Contributor

jesse99 commented Jun 23, 2012

The iter module has lots of handy stuff, but afaict there's no built-in way to iterate over numeric ranges. One thing that would be helpful is if, uint at least, had an impl something like

impl of iter::base_iter<uint> for uint
{
    fn each(blk: fn(&&uint) -> bool)
    {
        let mut i = 0u;
        while i < self
        {
            if (!blk(i))
            {
                ret;
            }
            i += 1u;
        }
    }

    fn size_hint() -> option<uint>
    {
        option::some(self)
    }
}

Even better would be a real range type. They seem to be a nice abstraction for collections and algorithms and they would make it simpler to iterate over ranges with non-zero lower bounds.

@jesse99
Copy link
Contributor Author

jesse99 commented Jun 23, 2012

Fwiw the motivation for this was turning a number N into a vector like ["a0", "a1", ..., "aN-1"]. Code I came up with is:

let names = iter::map_to_vec(N) {|i: uint| #fmt["a%?", i]};

@jesse99
Copy link
Contributor Author

jesse99 commented Jun 23, 2012

Some info on how ranges and iteration can work together: http://www.informit.com/articles/printerfriendly.aspx?p=1407357

@marshray
Copy link

Yeah Alexandrescu's range iterators are cool. He gave a good talk on them at Boostcon a few years ago: http://blip.tv/boostcon/boostcon-2009-keynote-2452140

I expect they were influential in the design of iterators in D. I played around with implementing them in C++. I'm not sure if Rust will have the heavy metaprogramming capability that makes them compile-time optimizeable, but that may be true of all types of iterators and generic operations. But they are very powerful and worth looking into.

@thestinger
Copy link
Contributor

There are now composable iterators in the iterator module (7158102). Lots of work to do, but they provide a useful subset of the functionality of libraries like Python's itertools and Boost Range. It could have an implementation for integer ranges.

@thestinger
Copy link
Contributor

I added a Counter iterator object providing pretty much anything someone could ever need when combined with take and takewhile.

RalfJung pushed a commit to RalfJung/rust that referenced this issue Dec 5, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
…t-lang#2885)

Improves the error reported to the user when the system kills CBMC to
reclaim memory.

Resolves rust-lang#2715
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.
Projects
None yet
Development

No branches or pull requests

3 participants