Skip to content

Commit 16e9f37

Browse files
committed
---
yaml --- r: 60439 b: refs/heads/auto c: 3122d80 h: refs/heads/master i: 60437: e780dac 60435: 23bc6ee 60431: 685ef65 v: v3
1 parent 3f68dd8 commit 16e9f37

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 54fbac505ed13c4afe193c8c4d6212df708e74d0
17+
refs/heads/auto: 3122d8027bfb38b76a916ef0e7be850da62f6e0b
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libcore/iterator.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub trait IteratorUtil<A> {
5353
fn last(&mut self) -> A;
5454
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
5555
fn count(&mut self) -> uint;
56+
fn all(&mut self, f: &fn(&A) -> bool) -> bool;
57+
fn any(&mut self, f: &fn(&A) -> bool) -> bool;
5658
}
5759

5860
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -204,6 +206,18 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
204206
/// Count the number of an iterator elemenrs
205207
#[inline(always)]
206208
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
209+
210+
#[inline(always)]
211+
fn all(&mut self, f: &fn(&A) -> bool) -> bool {
212+
for self.advance |x| { if !f(&x) { return false; } }
213+
return true;
214+
}
215+
216+
#[inline(always)]
217+
fn any(&mut self, f: &fn(&A) -> bool) -> bool {
218+
for self.advance |x| { if f(&x) { return true; } }
219+
return false;
220+
}
207221
}
208222

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

771+
#[test]
772+
fn test_all() {
773+
let v = ~&[1, 2, 3, 4, 5];
774+
assert!(v.iter().all(|&x| *x < 10));
775+
assert!(!v.iter().all(|&x| x.is_even()));
776+
assert!(!v.iter().all(|&x| *x > 100));
777+
assert!(v.slice(0, 0).iter().all(|_| fail!()));
778+
}
779+
780+
#[test]
781+
fn test_any() {
782+
let v = ~&[1, 2, 3, 4, 5];
783+
assert!(v.iter().any(|&x| *x < 10));
784+
assert!(v.iter().any(|&x| x.is_even()));
785+
assert!(!v.iter().any(|&x| *x > 100));
786+
assert!(!v.slice(0, 0).iter().any(|_| fail!()));
787+
}
757788
}

0 commit comments

Comments
 (0)