Skip to content

Commit ead600d

Browse files
committed
---
yaml --- r: 55164 b: refs/heads/snap-stage3 c: 6f18bb5 h: refs/heads/master v: v3
1 parent 8e181b7 commit ead600d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 5f13e9ccc2e3328d4cd8ca49f84e6840dd998346
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 7fed4800733805156f0d157e45b01de405c4b48e
4+
refs/heads/snap-stage3: 6f18bb550e1293e77281b1cc76f1830a4da2d355
55
refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/iter.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ much easier to implement.
4141
4242
*/
4343

44+
use option::{Option, Some, None};
45+
4446
pub trait Times {
4547
fn times(&self, it: &fn() -> bool);
4648
}
@@ -104,6 +106,27 @@ pub fn all<T>(predicate: &fn(T) -> bool, iter: &fn(f: &fn(T) -> bool)) -> bool {
104106
true
105107
}
106108

109+
/**
110+
* Return the first element where `predicate` returns `true`, otherwise return `Npne` if no element
111+
* is found.
112+
*
113+
* # Example:
114+
*
115+
* ~~~~
116+
* let xs = ~[1u, 2, 3, 4, 5, 6];
117+
* assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.each(f)).unwrap(), 4);
118+
* ~~~~
119+
*/
120+
#[inline(always)]
121+
pub fn find<T>(predicate: &fn(&T) -> bool, iter: &fn(f: &fn(T) -> bool)) -> Option<T> {
122+
for iter |x| {
123+
if predicate(&x) {
124+
return Some(x);
125+
}
126+
}
127+
None
128+
}
129+
107130
#[cfg(test)]
108131
mod tests {
109132
use super::*;
@@ -128,4 +151,10 @@ mod tests {
128151
assert!(all(|x: uint| x < 6, |f| uint::range(1, 6, f)));
129152
assert!(!all(|x: uint| x < 5, |f| uint::range(1, 6, f)));
130153
}
154+
155+
#[test]
156+
fn test_find() {
157+
let xs = ~[1u, 2, 3, 4, 5, 6];
158+
assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.each(f)).unwrap(), 4);
159+
}
131160
}

0 commit comments

Comments
 (0)