about summary refs log tree commit diff
path: root/src/libcore/iter.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-05-03 16:33:33 -0400
committerAlex Crichton <alex@alexcrichton.com>2013-05-10 19:20:20 -0400
commitb05aae2d4151a5985d58758fcd46037fb39a5fb9 (patch)
treec12b1b3738ade87f372a3388b13c698b1929d639 /src/libcore/iter.rs
parentcdc266e47d8ee63a1eaf29c775f2cbc5f3a61bb4 (diff)
downloadrust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.tar.gz
rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.zip
test: Use the new `for` protocol
Diffstat (limited to 'src/libcore/iter.rs')
-rw-r--r--src/libcore/iter.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index 0f064af657a..d5649d3dfd2 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -86,9 +86,34 @@ pub fn to_vec<T>(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
 #[cfg(not(stage0))]
 pub fn any<T>(predicate: &fn(T) -> bool,
               iter: &fn(f: &fn(T) -> bool) -> bool) -> bool {
-    // If the predicate returns true, we break. If we ever broke, then we found
-    // something
-    !iter(|x| !predicate(x))
+    for iter |x| {
+        if predicate(x) {
+            return true;
+        }
+    }
+    return false;
+}
+
+/**
+ * Return true if `predicate` is true for all values yielded by an internal iterator.
+ *
+ * # Example:
+ *
+ * ~~~~
+ * assert!(all(|&x: &uint| x < 6, |f| uint::range(1, 6, f)));
+ * assert!(!all(|&x: &uint| x < 5, |f| uint::range(1, 6, f)));
+ * ~~~~
+ */
+#[inline(always)]
+#[cfg(stage0)]
+pub fn all<T>(predicate: &fn(T) -> bool,
+              iter: &fn(f: &fn(T) -> bool)) -> bool {
+    for iter |x| {
+        if !predicate(x) {
+            return false;
+        }
+    }
+    return true;
 }
 
 /**