Skip to content

Commit 3aaf321

Browse files
committed
---
yaml --- r: 61439 b: refs/heads/try c: 3122d80 h: refs/heads/master i: 61437: 483eba1 61435: e83e568 61431: 774808c 61423: 9506b71 61407: dd7e0ae 61375: 806fed7 61311: 9f2e87b 61183: 0d85eca 60927: 97020a4 60415: 3e4adb0 59391: 60c171b 57343: b593331 v: v3
1 parent b4f0f4b commit 3aaf321

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
@@ -2,7 +2,7 @@
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2d28d645422c1617be58c8ca7ad9a457264ca850
5-
refs/heads/try: 54fbac505ed13c4afe193c8c4d6212df708e74d0
5+
refs/heads/try: 3122d8027bfb38b76a916ef0e7be850da62f6e0b
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/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)