Skip to content

make iterator objects a bit nicer to use #5901

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

Merged
merged 2 commits into from
Apr 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion RELEASES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Version 0.7 (July 2013)
-----------------------

* ??? changes, numerous bugfixes

* Semantic changes
* The `self` parameter no longer implicitly means `&'self self`, and can be explicitly marked
with a lifetime.

* Libraries
* New `core::iterator` module for external iterator objects

Version 0.6 (April 2013)
---------------------------
------------------------

* ~2100 changes, numerous bugfixes

Expand Down
69 changes: 37 additions & 32 deletions src/libcore/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -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)> {
Expand All @@ -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 {
Expand All @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -1005,7 +1005,7 @@ mod test_treemap {
}
}

for advance(&mut b) |x| {
for b.advance |x| {
assert!(expected[i] == x);
i += 1;
}
Expand Down Expand Up @@ -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();
Expand Down