diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-05-03 16:33:33 -0400 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-05-10 19:20:20 -0400 |
| commit | b05aae2d4151a5985d58758fcd46037fb39a5fb9 (patch) | |
| tree | c12b1b3738ade87f372a3388b13c698b1929d639 /src/libcore | |
| parent | cdc266e47d8ee63a1eaf29c775f2cbc5f3a61bb4 (diff) | |
| download | rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.tar.gz rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.zip | |
test: Use the new `for` protocol
Diffstat (limited to 'src/libcore')
| -rwxr-xr-x | src/libcore/core | bin | 0 -> 9455580 bytes | |||
| -rw-r--r-- | src/libcore/iter.rs | 31 | ||||
| -rw-r--r-- | src/libcore/str.rs | 4 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 28 |
4 files changed, 56 insertions, 7 deletions
diff --git a/src/libcore/core b/src/libcore/core new file mode 100755 index 00000000000..790c07db685 --- /dev/null +++ b/src/libcore/core Binary files differdiff --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; } /** diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 4ab6f659e9c..d7b27f53e7a 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -3404,7 +3404,7 @@ mod tests { let lf = ~"\nMary had a little lamb\nLittle lamb\n"; let crlf = ~"\r\nMary had a little lamb\r\nLittle lamb\r\n"; - fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) { + fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } assert!(vec::all2(v, u, |a,b| a == b)); @@ -3424,7 +3424,7 @@ mod tests { #[test] fn test_words () { - fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) { + fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) { let mut v = ~[]; for f(s) |s| { v.push(s.to_owned()) } assert!(vec::all2(v, u, |a,b| a == b)); diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index e1947b77473..7eba2cbf0cc 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1744,6 +1744,7 @@ pub fn each_permutation<T:Copy>(v: &[T], put: &fn(ts: &[T]) -> bool) -> bool { * ~~~ * */ +#[cfg(stage0)] pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) { assert!(1u <= n); if n > v.len() { return; } @@ -1751,6 +1752,29 @@ pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) { if !it(v.slice(i, i + n)) { return } } } +/** + * Iterate over all contiguous windows of length `n` of the vector `v`. + * + * # Example + * + * Print the adjacent pairs of a vector (i.e. `[1,2]`, `[2,3]`, `[3,4]`) + * + * ~~~ + * for windowed(2, &[1,2,3,4]) |v| { + * io::println(fmt!("%?", v)); + * } + * ~~~ + * + */ +#[cfg(not(stage0))] +pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) -> bool { + assert!(1u <= n); + if n > v.len() { return true; } + for uint::range(0, v.len() - n + 1) |i| { + if !it(v.slice(i, i + n)) { return false; } + } + return true; +} /** * Work with the buffer of a vector. @@ -4566,7 +4590,7 @@ mod tests { } i += 0; false - } + }; } #[test] @@ -4581,7 +4605,7 @@ mod tests { } i += 0; false - } + }; } #[test] |
