Skip to content

Commit 07fde8f

Browse files
committed
---
yaml --- r: 55199 b: refs/heads/snap-stage3 c: db24514 h: refs/heads/master i: 55197: 9c649b3 55195: 4a5caf9 55191: abc4298 55183: 447472d 55167: 65ba145 v: v3
1 parent 6c0845c commit 07fde8f

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-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: 89f419370c543a8d3bc905c638c60c293401b0fa
4+
refs/heads/snap-stage3: db2451477b4d830b2fa7b19761eb4009089205a0
55
refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/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;

branches/snap-stage3/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)