[rust-dev] iter library

Joe Groff arcata at gmail.com
Fri Apr 27 20:02:28 PDT 2012


On Apr 27, 2012, at 7:05 PM, Niko Matsakis <niko at alum.mit.edu> wrote:

>
> Later, I would like to add str, map, and a variety of other types.  For types (like str and map) where there are multiple possibilities for how to iterate, my plan is to use wrapper types like so:
>
>    enum keys<K,V,M:map<K,V>> = &M;
>
> This wrapper would allow you to iterate over the keys in a map.  You would use it something like:
>
>    for keys(&map).each { |k| ... }
>
> This also allows things like
>
>     keys(&map).map_to_vec { |k| ... }

Is the wrapper type necessary? I thought named implementations were
intended to allow multiple implementations without wrapping.

> Another example where I think such wrapper types would be helpful is for iterating over two slices in parallel.  I planned to have an enum like:
>
>    enum zip<A,B> {
>        zip([A]/&, [B]/&)
>    }
>
> which would implement the iterable interface for the type (&A, &B).  And so forth.

Would the each() method of an iterator be resumable if you called it
again after stopping a previous iteration? If so, you could implement
a generic zip over any two iterators.


> ## Possible far future plans
>
> If we added support for higher-kinded types, we could support a `map()` method in the iteration trait.  But until then we have `map_to_vec()`, which always results in a type of `[A]`.  Maybe we should find a shorter name, to just do `iterable.to_vec().map()`.  I dunno.

Could you implement a map<I,O,fn I -> O, iterable<I>> : iterable<O>
adapter without higher kinds?

-Joe


More information about the Rust-dev mailing list