Skip to content

Commit db24514

Browse files
committed
auto merge of #6131 : thestinger/rust/new_iter, r=graydon
2 parents 89f4193 + a6eaa3b commit db24514

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/libcore/char.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! Utilities for manipulating the char type
1212
13+
#[cfg(notest)]
1314
use cmp::Ord;
1415
use option::{None, Option, Some};
1516
use str;

src/libcore/iter.rs

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

44+
use cmp::Ord;
45+
use option::{Option, Some, None};
46+
4447
pub trait Times {
4548
fn times(&self, it: &fn() -> bool);
4649
}
@@ -104,6 +107,78 @@ pub fn all<T>(predicate: &fn(T) -> bool, iter: &fn(f: &fn(T) -> bool)) -> bool {
104107
true
105108
}
106109

110+
/**
111+
* Return the first element where `predicate` returns `true`. Return `None` if no element 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+
130+
/**
131+
* Return the largest item yielded by an iterator. Return `None` if the iterator is empty.
132+
*
133+
* # Example:
134+
*
135+
* ~~~~
136+
* let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
137+
* assert_eq!(max(|f| xs.each(f)).unwrap(), &15);
138+
* ~~~~
139+
*/
140+
#[inline]
141+
pub fn max<T: Ord>(iter: &fn(f: &fn(T) -> bool)) -> Option<T> {
142+
let mut result = None;
143+
for iter |x| {
144+
match result {
145+
Some(ref mut y) => {
146+
if x > *y {
147+
*y = x;
148+
}
149+
}
150+
None => result = Some(x)
151+
}
152+
}
153+
result
154+
}
155+
156+
/**
157+
* Return the smallest item yielded by an iterator. Return `None` if the iterator is empty.
158+
*
159+
* # Example:
160+
*
161+
* ~~~~
162+
* let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
163+
* assert_eq!(max(|f| xs.each(f)).unwrap(), &-5);
164+
* ~~~~
165+
*/
166+
#[inline]
167+
pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool)) -> Option<T> {
168+
let mut result = None;
169+
for iter |x| {
170+
match result {
171+
Some(ref mut y) => {
172+
if x < *y {
173+
*y = x;
174+
}
175+
}
176+
None => result = Some(x)
177+
}
178+
}
179+
result
180+
}
181+
107182
#[cfg(test)]
108183
mod tests {
109184
use super::*;
@@ -128,4 +203,22 @@ mod tests {
128203
assert!(all(|x: uint| x < 6, |f| uint::range(1, 6, f)));
129204
assert!(!all(|x: uint| x < 5, |f| uint::range(1, 6, f)));
130205
}
206+
207+
#[test]
208+
fn test_find() {
209+
let xs = ~[1u, 2, 3, 4, 5, 6];
210+
assert_eq!(*find(|& &x: & &uint| x > 3, |f| xs.each(f)).unwrap(), 4);
211+
}
212+
213+
#[test]
214+
fn test_max() {
215+
let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
216+
assert_eq!(max(|f| xs.each(f)).unwrap(), &15);
217+
}
218+
219+
#[test]
220+
fn test_min() {
221+
let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
222+
assert_eq!(min(|f| xs.each(f)).unwrap(), &-5);
223+
}
131224
}

0 commit comments

Comments
 (0)