core: Make range follow the for loop protocol

This commit is contained in:
Brian Anderson
2012-05-26 00:32:08 -07:00
parent 5281db2bc2
commit 432c6cbde9
46 changed files with 102 additions and 88 deletions

View File

@@ -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"]

View File

@@ -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 };
}

View File

@@ -202,7 +202,7 @@ impl extensions for rng {
fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] {
let mut r = [];
for v.each {|item|
uint::range(0u, item.weight) {|_i|
for uint::range(0u, item.weight) {|_i|
r += [item.item];
}
}

View File

@@ -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"]

View File

@@ -892,7 +892,7 @@ Both vectors must have the same length
#[inline]
fn iter2<U, T>(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])
}
}