diff options
| author | bors <bors@rust-lang.org> | 2013-06-15 22:16:28 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-15 22:16:28 -0700 |
| commit | 5572023cd868c5b6045bc91355cd21465c59ff6d (patch) | |
| tree | 0929e1a81a4324b218ab1e8d8663f04ab313e20a /src/libstd | |
| parent | b9119edc55287bb1a9b5609bdef84001c3341e22 (diff) | |
| parent | 79cd2dbe72781e14adb321483cfed1d861a598ce (diff) | |
| download | rust-5572023cd868c5b6045bc91355cd21465c59ff6d.tar.gz rust-5572023cd868c5b6045bc91355cd21465c59ff6d.zip | |
auto merge of #7162 : thestinger/rust/iterator, r=brson
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iterator.rs | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index e65904a6899..a505c552359 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -308,6 +308,12 @@ pub trait IteratorUtil<A> { /// assert!(!it.any_(|&x| *x == 3)); /// ~~~ fn any_(&mut self, f: &fn(A) -> bool) -> bool; + + /// Return the first element satisfying the specified predicate + fn find_(&mut self, predicate: &fn(&A) -> bool) -> Option<A>; + + /// Return the index of the first element satisfying the specified predicate + fn position_(&mut self, predicate: &fn(A) -> bool) -> Option<uint>; } /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also @@ -421,7 +427,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { None => { break; } } } - return accum; + accum } /// Count the number of items yielded by an iterator @@ -431,13 +437,35 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { #[inline(always)] fn all(&mut self, f: &fn(A) -> bool) -> bool { for self.advance |x| { if !f(x) { return false; } } - return true; + true } #[inline(always)] fn any_(&mut self, f: &fn(A) -> bool) -> bool { for self.advance |x| { if f(x) { return true; } } - return false; + false + } + + /// Return the first element satisfying the specified predicate + #[inline(always)] + fn find_(&mut self, predicate: &fn(&A) -> bool) -> Option<A> { + for self.advance |x| { + if predicate(&x) { return Some(x) } + } + None + } + + /// Return the index of the first element satisfying the specified predicate + #[inline] + fn position_(&mut self, predicate: &fn(A) -> bool) -> Option<uint> { + let mut i = 0; + for self.advance |x| { + if predicate(x) { + return Some(i); + } + i += 1; + } + None } } @@ -1055,4 +1083,20 @@ mod tests { assert!(!v.iter().any_(|&x| x > 100)); assert!(!v.slice(0, 0).iter().any_(|_| fail!())); } + + #[test] + fn test_find() { + let v = &[1, 3, 9, 27, 103, 14, 11]; + assert_eq!(*v.iter().find_(|x| *x & 1 == 0).unwrap(), 14); + assert_eq!(*v.iter().find_(|x| *x % 3 == 0).unwrap(), 3); + assert!(v.iter().find_(|x| *x % 12 == 0).is_none()); + } + + #[test] + fn test_position() { + let v = &[1, 3, 9, 27, 103, 14, 11]; + assert_eq!(v.iter().position_(|x| *x & 1 == 0).unwrap(), 5); + assert_eq!(v.iter().position_(|x| *x % 3 == 0).unwrap(), 1); + assert!(v.iter().position_(|x| *x % 12 == 0).is_none()); + } } |
