]> git.lizzy.rs Git - rust.git/commitdiff
iter: add a find function
authorDaniel Micay <danielmicay@gmail.com>
Tue, 30 Apr 2013 17:01:20 +0000 (13:01 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Tue, 30 Apr 2013 17:07:14 +0000 (13:07 -0400)
src/libcore/iter.rs

index 7476531ef944cdd301d52bd81859b3f1b841630b..6f3c6890228acdbf282eb23b39e480945054e6da 100644 (file)
@@ -41,6 +41,8 @@
 
 */
 
+use option::{Option, Some, None};
+
 pub trait Times {
     fn times(&self, it: &fn() -> bool);
 }
@@ -104,6 +106,27 @@ pub fn all<T>(predicate: &fn(T) -> bool, iter: &fn(f: &fn(T) -> bool)) -> bool {
     true
 }
 
+/**
+ * Return the first element where `predicate` returns `true`, otherwise return `Npne` if no element
+ * is found.
+ *
+ * # Example:
+ *
+ * ~~~~
+ * let xs = ~[1u, 2, 3, 4, 5, 6];
+ * assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.each(f)).unwrap(), 4);
+ * ~~~~
+ */
+#[inline(always)]
+pub fn find<T>(predicate: &fn(&T) -> bool, iter: &fn(f: &fn(T) -> bool)) -> Option<T> {
+    for iter |x| {
+        if predicate(&x) {
+            return Some(x);
+        }
+    }
+    None
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -128,4 +151,10 @@ fn test_all() {
         assert!(all(|x: uint| x < 6, |f| uint::range(1, 6, f)));
         assert!(!all(|x: uint| x < 5, |f| uint::range(1, 6, f)));
     }
+
+    #[test]
+    fn test_find() {
+        let xs = ~[1u, 2, 3, 4, 5, 6];
+        assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.each(f)).unwrap(), 4);
+    }
 }