From 432c6cbde93ca525cc33fd17873a0557dffa07c5 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 26 May 2012 00:32:08 -0700 Subject: core: Make range follow the for loop protocol --- src/libcore/int-template.rs | 7 +++++-- src/libcore/priv.rs | 6 +++--- src/libcore/rand.rs | 2 +- src/libcore/uint-template.rs | 7 +++++-- src/libcore/vec.rs | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index ac1cc3bbf8b..07c3cc9011e 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -36,9 +36,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: T, hi: T, it: fn(T)) { +fn range(lo: T, hi: T, it: fn(T) -> bool) { let mut i = lo; - while i < hi { it(i); i += 1 as T; } + while i < hi { + if !it(i) { break } + i += 1 as T; + } } #[doc = "Computes the bitwise complement"] diff --git a/src/libcore/priv.rs b/src/libcore/priv.rs index 6fa20ce0804..d43fc27a019 100644 --- a/src/libcore/priv.rs +++ b/src/libcore/priv.rs @@ -127,12 +127,12 @@ fn test_from_global_chan2() unsafe { // Spawn a bunch of tasks that all want to compete to // create the global channel - uint::range(0u, 10u) {|i| + for uint::range(0u, 10u) {|i| task::spawn() {|| let ch = chan_from_global_ptr( globchanp, task::builder) {|po| - uint::range(0u, 10u) {|_j| + for uint::range(0u, 10u) {|_j| let ch = comm::recv(po); comm::send(ch, {i}); } @@ -147,7 +147,7 @@ fn test_from_global_chan2() unsafe { } // There should be only one winner let mut winners = 0u; - uint::range(0u, 10u) {|_i| + for uint::range(0u, 10u) {|_i| let res = comm::recv(resultpo); if res { winners += 1u }; } diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index dedcf991670..592124767ff 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -202,7 +202,7 @@ impl extensions for rng { fn weighted_vec(v: [weighted]) -> [T] { let mut r = []; for v.each {|item| - uint::range(0u, item.weight) {|_i| + for uint::range(0u, item.weight) {|_i| r += [item.item]; } } diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index f9bffe3cc0f..e34fb145336 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -35,9 +35,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: T, hi: T, it: fn(T)) { +fn range(lo: T, hi: T, it: fn(T) -> bool) { let mut i = lo; - while i < hi { it(i); i += 1 as T; } + while i < hi { + if !it(i) { break } + i += 1 as T; + } } #[doc = "Computes the bitwise complement"] diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index c72cef85cd1..fdc8b1ed248 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -892,7 +892,7 @@ Both vectors must have the same length #[inline] fn iter2(v1: [const U], v2: [const T], f: fn(U, T)) { assert len(v1) == len(v2); - uint::range(0u, len(v1)) {|i| + for uint::range(0u, len(v1)) {|i| f(v1[i], v2[i]) } } -- cgit 1.4.1-3-g733a5