Skip to content

Commit

Permalink
libcore: Add IteratorUtil::all, any method
Browse files Browse the repository at this point in the history
  • Loading branch information
gifnksm committed May 17, 2013
1 parent 54fbac5 commit 3122d80
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/libcore/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub trait IteratorUtil<A> {
fn last(&mut self) -> A;
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
fn count(&mut self) -> uint;
fn all(&mut self, f: &fn(&A) -> bool) -> bool;
fn any(&mut self, f: &fn(&A) -> bool) -> bool;
}

/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
Expand Down Expand Up @@ -204,6 +206,18 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
/// Count the number of an iterator elemenrs
#[inline(always)]
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }

#[inline(always)]
fn all(&mut self, f: &fn(&A) -> bool) -> bool {
for self.advance |x| { if !f(&x) { return false; } }
return true;
}

#[inline(always)]
fn any(&mut self, f: &fn(&A) -> bool) -> bool {
for self.advance |x| { if f(&x) { return true; } }
return false;
}
}

pub trait AdditiveIterator<A> {
Expand Down Expand Up @@ -754,4 +768,21 @@ mod tests {
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).min(), None);
}

#[test]
fn test_all() {
let v = ~&[1, 2, 3, 4, 5];
assert!(v.iter().all(|&x| *x < 10));
assert!(!v.iter().all(|&x| x.is_even()));
assert!(!v.iter().all(|&x| *x > 100));
assert!(v.slice(0, 0).iter().all(|_| fail!()));
}

#[test]
fn test_any() {
let v = ~&[1, 2, 3, 4, 5];
assert!(v.iter().any(|&x| *x < 10));
assert!(v.iter().any(|&x| x.is_even()));
assert!(!v.iter().any(|&x| *x > 100));
assert!(!v.slice(0, 0).iter().any(|_| fail!()));
}
}

5 comments on commit 3122d80

@bors
Copy link
Contributor

@bors bors commented on 3122d80 May 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 3122d80 May 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gifnksm/rust/iterator-utils = 3122d80 into auto

@bors
Copy link
Contributor

@bors bors commented on 3122d80 May 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gifnksm/rust/iterator-utils = 3122d80 merged ok, testing candidate = 799d9fa

@bors
Copy link
Contributor

@bors bors commented on 3122d80 May 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 3122d80 May 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 799d9fa

Please sign in to comment.