diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs index e7a2f3a392888..fcb5102d4c07c 100644 --- a/src/libcore/iterator.rs +++ b/src/libcore/iterator.rs @@ -12,20 +12,46 @@ use prelude::*; -pub trait Iterator { +pub trait Iterator { /// Advance the iterator and return the next value. Return `None` when the end is reached. - fn next(&mut self) -> Option; + fn next(&mut self) -> Option; } -/// A shim implementing the `for` loop iteration protocol for iterator objects -#[inline] -pub fn advance>(iter: &mut U, f: &fn(T) -> bool) { - loop { - match iter.next() { - Some(x) => { - if !f(x) { return } +pub trait IteratorUtil { + fn zip>(self, other: U) -> ZipIterator; + // 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> IteratorUtil for T { + #[inline(always)] + fn zip>(self, other: U) -> ZipIterator { + 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 { priv b: U } -pub impl, U: Iterator> ZipIterator { - #[inline(always)] - fn new(a: T, b: U) -> ZipIterator { - ZipIterator{a: a, b: b} - } -} - impl, U: Iterator> Iterator<(A, B)> for ZipIterator { #[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> 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> Iterator for FilterIterator<'self, A, T> { #[inline] fn next(&mut self) -> Option { - 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> 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> Iterator for MapIterator<'self, A, B, T> { #[inline] fn next(&mut self) -> Option { diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index bc8cbaa0825ce..ac887c7fdc41d 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -996,7 +996,7 @@ mod test_treemap { (&x5, &y5)]; let mut i = 0; - for advance(&mut b) |x| { + for b.advance |x| { assert!(expected[i] == x); i += 1; @@ -1005,7 +1005,7 @@ mod test_treemap { } } - for advance(&mut b) |x| { + for b.advance |x| { assert!(expected[i] == x); i += 1; } @@ -1209,7 +1209,7 @@ mod test_set { let x = x; let y = y; - let mut z = ZipIterator::new(x.iter(), y.iter()); + let mut z = x.iter().zip(y.iter()); // FIXME: #5801: this needs a type hint to compile... let result: Option<(&uint, & &'static str)> = z.next();