diff options
| author | bors <bors@rust-lang.org> | 2013-04-15 21:03:55 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-15 21:03:55 -0700 |
| commit | 76e77af380e267ecd07c061aca6f428b42f8d35a (patch) | |
| tree | db823f0b055d6e06fb268fc7e0eb29de959fe866 /src/libcore | |
| parent | 16e8af9e477fd5a6e7dcf668840144a97e3b1410 (diff) | |
| parent | f82c96446f7577cc29c71ed793a531c9189e7039 (diff) | |
| download | rust-76e77af380e267ecd07c061aca6f428b42f8d35a.tar.gz rust-76e77af380e267ecd07c061aca6f428b42f8d35a.zip | |
auto merge of #5901 : thestinger/rust/iterator, r=sanxiyn
Can now use them like `x.transform(|i| i + 3).zip(y.filter(|i| i % 2)`.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iterator.rs | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs index e7a2f3a3928..fcb5102d4c0 100644 --- a/src/libcore/iterator.rs +++ b/src/libcore/iterator.rs @@ -12,20 +12,46 @@ use prelude::*; -pub trait Iterator<T> { +pub trait Iterator<A> { /// Advance the iterator and return the next value. Return `None` when the end is reached. - fn next(&mut self) -> Option<T>; + fn next(&mut self) -> Option<A>; } -/// A shim implementing the `for` loop iteration protocol for iterator objects -#[inline] -pub fn advance<T, U: Iterator<T>>(iter: &mut U, f: &fn(T) -> bool) { - loop { - match iter.next() { - Some(x) => { - if !f(x) { return } +pub trait IteratorUtil<A> { + fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>; + // FIXME: #5898: should be called map + fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>; + fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>; + fn advance(&mut self, f: &fn(A) -> bool); +} + +impl<A, T: Iterator<A>> IteratorUtil<A> for T { + #[inline(always)] + fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> { + ZipIterator{a: self, b: other} + } + + // FIXME: #5898: should be called map + #[inline(always)] + fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> { + MapIterator{iter: self, f: f} + } + + #[inline(always)] + fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> { + FilterIterator{iter: self, predicate: predicate} + } + + /// A shim implementing the `for` loop iteration protocol for iterator objects + #[inline] + fn advance(&mut self, f: &fn(A) -> bool) { + loop { + match self.next() { + Some(x) => { + if !f(x) { return } + } + None => return } - None => return } } } @@ -35,13 +61,6 @@ pub struct ZipIterator<T, U> { priv b: U } -pub impl<A, B, T: Iterator<A>, U: Iterator<B>> ZipIterator<T, U> { - #[inline(always)] - fn new(a: T, b: U) -> ZipIterator<T, U> { - ZipIterator{a: a, b: b} - } -} - impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> { #[inline] fn next(&mut self) -> Option<(A, B)> { @@ -57,17 +76,10 @@ pub struct FilterIterator<'self, A, T> { priv predicate: &'self fn(&A) -> bool } -pub impl<'self, A, T: Iterator<A>> FilterIterator<'self, A, T> { - #[inline(always)] - fn new(iter: T, predicate: &'self fn(&A) -> bool) -> FilterIterator<'self, A, T> { - FilterIterator{iter: iter, predicate: predicate} - } -} - impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> { #[inline] fn next(&mut self) -> Option<A> { - for advance(self) |x| { + for self.iter.advance |x| { if (self.predicate)(&x) { return Some(x); } else { @@ -83,13 +95,6 @@ pub struct MapIterator<'self, A, B, T> { priv f: &'self fn(A) -> B } -pub impl<'self, A, B, T: Iterator<A>> MapIterator<'self, A, B, T> { - #[inline(always)] - fn new(iter: T, f: &'self fn(A) -> B) -> MapIterator<'self, A, B, T> { - MapIterator{iter: iter, f: f} - } -} - impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> { #[inline] fn next(&mut self) -> Option<B> { |
