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

@ -277,7 +277,7 @@ fn check_expected_errors(expected_errors: [errors::expected_error],
} }
} }
uint::range(0u, vec::len(found_flags)) {|i| for uint::range(0u, vec::len(found_flags)) {|i|
if !found_flags[i] { if !found_flags[i] {
let ee = expected_errors[i]; let ee = expected_errors[i];
fatal_procres(#fmt["expected %s on line %u not found: %s", fatal_procres(#fmt["expected %s on line %u not found: %s",

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 } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
#[doc = "Iterate over the range [`lo`..`hi`)"] #[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; 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"] #[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 // Spawn a bunch of tasks that all want to compete to
// create the global channel // create the global channel
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
task::spawn() {|| task::spawn() {||
let ch = chan_from_global_ptr( let ch = chan_from_global_ptr(
globchanp, task::builder) {|po| globchanp, task::builder) {|po|
uint::range(0u, 10u) {|_j| for uint::range(0u, 10u) {|_j|
let ch = comm::recv(po); let ch = comm::recv(po);
comm::send(ch, {i}); comm::send(ch, {i});
} }
@ -147,7 +147,7 @@ fn test_from_global_chan2() unsafe {
} }
// There should be only one winner // There should be only one winner
let mut winners = 0u; let mut winners = 0u;
uint::range(0u, 10u) {|_i| for uint::range(0u, 10u) {|_i|
let res = comm::recv(resultpo); let res = comm::recv(resultpo);
if res { winners += 1u }; if res { winners += 1u };
} }

View File

@ -202,7 +202,7 @@ impl extensions for rng {
fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] { fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] {
let mut r = []; let mut r = [];
for v.each {|item| for v.each {|item|
uint::range(0u, item.weight) {|_i| for uint::range(0u, item.weight) {|_i|
r += [item.item]; 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 } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
#[doc = "Iterate over the range [`lo`..`hi`)"] #[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; 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"] #[doc = "Computes the bitwise complement"]

View File

@ -892,7 +892,7 @@ Both vectors must have the same length
#[inline] #[inline]
fn iter2<U, T>(v1: [const U], v2: [const T], f: fn(U, T)) { fn iter2<U, T>(v1: [const U], v2: [const T], f: fn(U, T)) {
assert len(v1) == len(v2); assert len(v1) == len(v2);
uint::range(0u, len(v1)) {|i| for uint::range(0u, len(v1)) {|i|
f(v1[i], v2[i]) f(v1[i], v2[i])
} }
} }

View File

@ -183,7 +183,7 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
let lo = lookup_char_pos(cm, sp.lo); let lo = lookup_char_pos(cm, sp.lo);
let hi = lookup_char_pos(cm, sp.hi); let hi = lookup_char_pos(cm, sp.hi);
let mut lines = []; let mut lines = [];
uint::range(lo.line - 1u, hi.line as uint) {|i| lines += [i]; }; for uint::range(lo.line - 1u, hi.line as uint) {|i| lines += [i]; };
ret @{file: lo.file, lines: lines}; ret @{file: lo.file, lines: lines};
} }

View File

@ -193,7 +193,7 @@ fn finish<T: qq_helper>
let qcx = gather_anti_quotes(sp.lo, node); let qcx = gather_anti_quotes(sp.lo, node);
let cx = qcx; let cx = qcx;
uint::range(1u, cx.gather.len()) {|i| for uint::range(1u, cx.gather.len()) {|i|
assert cx.gather[i-1u].lo < cx.gather[i].lo; assert cx.gather[i-1u].lo < cx.gather[i].lo;
// ^^ check that the vector is sorted // ^^ check that the vector is sorted
assert cx.gather[i-1u].hi <= cx.gather[i].lo; assert cx.gather[i-1u].hi <= cx.gather[i].lo;

View File

@ -46,7 +46,7 @@ fn process(v0: bitv, v1: bitv, op: fn(uint, uint) -> uint) -> bool {
assert (vec::len(v0.storage) == len); assert (vec::len(v0.storage) == len);
assert (v0.nbits == v1.nbits); assert (v0.nbits == v1.nbits);
let mut changed = false; let mut changed = false;
uint::range(0u, len) {|i| for uint::range(0u, len) {|i|
let w0 = v0.storage[i]; let w0 = v0.storage[i];
let w1 = v1.storage[i]; let w1 = v1.storage[i];
let w = op(w0, w1); let w = op(w0, w1);
@ -90,7 +90,7 @@ fn assign(v0: bitv, v1: bitv) -> bool {
fn clone(v: bitv) -> bitv { fn clone(v: bitv) -> bitv {
let storage = vec::to_mut(vec::from_elem(v.nbits / uint_bits + 1u, 0u)); let storage = vec::to_mut(vec::from_elem(v.nbits / uint_bits + 1u, 0u));
let len = vec::len(v.storage); let len = vec::len(v.storage);
uint::range(0u, len) {|i| storage[i] = v.storage[i]; }; for uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
ret @{storage: storage, nbits: v.nbits}; ret @{storage: storage, nbits: v.nbits};
} }
@ -121,17 +121,17 @@ fn equal(v0: bitv, v1: bitv) -> bool {
#[doc = "Set all bits to 0"] #[doc = "Set all bits to 0"]
fn clear(v: bitv) { fn clear(v: bitv) {
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; }; for uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
} }
#[doc = "Set all bits to 1"] #[doc = "Set all bits to 1"]
fn set_all(v: bitv) { fn set_all(v: bitv) {
uint::range(0u, v.nbits) {|i| set(v, i, true); }; for uint::range(0u, v.nbits) {|i| set(v, i, true); };
} }
#[doc = "Invert all bits"] #[doc = "Invert all bits"]
fn invert(v: bitv) { fn invert(v: bitv) {
uint::range(0u, vec::len(v.storage)) {|i| for uint::range(0u, vec::len(v.storage)) {|i|
v.storage[i] = !v.storage[i]; v.storage[i] = !v.storage[i];
}; };
} }

View File

@ -158,13 +158,13 @@ fn concat(v: [rope]) -> rope {
let mut len = vec::len(v); let mut len = vec::len(v);
if len == 0u { ret node::empty; } if len == 0u { ret node::empty; }
let ropes = vec::to_mut(vec::from_elem(len, v[0])); let ropes = vec::to_mut(vec::from_elem(len, v[0]));
uint::range(1u, len) {|i| for uint::range(1u, len) {|i|
ropes[i] = v[i]; ropes[i] = v[i];
} }
//Merge progresively //Merge progresively
while len > 1u { while len > 1u {
uint::range(0u, len/2u) {|i| for uint::range(0u, len/2u) {|i|
ropes[i] = append_rope(ropes[2u*i], ropes[2u*i+1u]); ropes[i] = append_rope(ropes[2u*i], ropes[2u*i+1u]);
} }
if len%2u != 0u { if len%2u != 0u {
@ -1352,19 +1352,19 @@ mod tests {
fn char_at1() { fn char_at1() {
//Generate a large rope //Generate a large rope
let mut r = of_str(@ "123456789"); let mut r = of_str(@ "123456789");
uint::range(0u, 10u){|_i| for uint::range(0u, 10u){|_i|
r = append_rope(r, r); r = append_rope(r, r);
} }
//Copy it in the slowest possible way //Copy it in the slowest possible way
let mut r2 = empty(); let mut r2 = empty();
uint::range(0u, char_len(r)){|i| for uint::range(0u, char_len(r)){|i|
r2 = append_char(r2, char_at(r, i)); r2 = append_char(r2, char_at(r, i));
} }
assert eq(r, r2); assert eq(r, r2);
let mut r3 = empty(); let mut r3 = empty();
uint::range(0u, char_len(r)){|i| for uint::range(0u, char_len(r)){|i|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u)); r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
} }
assert eq(r, r3); assert eq(r, r3);
@ -1385,7 +1385,7 @@ mod tests {
//Generate a reasonable rope //Generate a reasonable rope
let chunk = of_str(@ "123456789"); let chunk = of_str(@ "123456789");
let mut r = empty(); let mut r = empty();
uint::range(0u, 10u){|_i| for uint::range(0u, 10u){|_i|
r = append_rope(r, chunk); r = append_rope(r, chunk);
} }

View File

@ -137,7 +137,7 @@ fn get_relative_to(abs1: path::path, abs2: path::path) -> path::path {
} }
let mut path = []; let mut path = [];
uint::range(start_idx, len1 - 1u) {|_i| path += [".."]; }; for uint::range(start_idx, len1 - 1u) {|_i| path += [".."]; };
path += vec::slice(split2, start_idx, len2 - 1u); path += vec::slice(split2, start_idx, len2 - 1u);

View File

@ -840,7 +840,7 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
fn create_index<T: copy>(index: [entry<T>], hash_fn: fn@(T) -> uint) -> fn create_index<T: copy>(index: [entry<T>], hash_fn: fn@(T) -> uint) ->
[@[entry<T>]] { [@[entry<T>]] {
let mut buckets: [@mut [entry<T>]] = []; let mut buckets: [@mut [entry<T>]] = [];
uint::range(0u, 256u) {|_i| buckets += [@mut []]; }; for uint::range(0u, 256u) {|_i| buckets += [@mut []]; };
for index.each {|elt| for index.each {|elt|
let h = hash_fn(elt.val); let h = hash_fn(elt.val);
*buckets[h % 256u] += [elt]; *buckets[h % 256u] += [elt];

View File

@ -595,7 +595,7 @@ class liveness {
fn indices(ln: live_node, op: fn(uint)) { fn indices(ln: live_node, op: fn(uint)) {
let node_base_idx = self.idx(ln, variable(0u)); let node_base_idx = self.idx(ln, variable(0u));
uint::range(0u, self.ir.num_vars) { |var_idx| for uint::range(0u, self.ir.num_vars) { |var_idx|
op(node_base_idx + var_idx) op(node_base_idx + var_idx)
} }
} }
@ -604,7 +604,7 @@ class liveness {
op: fn(uint, uint)) { op: fn(uint, uint)) {
let node_base_idx = self.idx(ln, variable(0u)); let node_base_idx = self.idx(ln, variable(0u));
let succ_base_idx = self.idx(succ_ln, variable(0u)); let succ_base_idx = self.idx(succ_ln, variable(0u));
uint::range(0u, self.ir.num_vars) { |var_idx| for uint::range(0u, self.ir.num_vars) { |var_idx|
op(node_base_idx + var_idx, succ_base_idx + var_idx); op(node_base_idx + var_idx, succ_base_idx + var_idx);
} }
} }
@ -613,7 +613,7 @@ class liveness {
ln: live_node, ln: live_node,
test: fn(uint) -> live_node) { test: fn(uint) -> live_node) {
let node_base_idx = self.idx(ln, variable(0u)); let node_base_idx = self.idx(ln, variable(0u));
uint::range(0u, self.ir.num_vars) { |var_idx| for uint::range(0u, self.ir.num_vars) { |var_idx|
let idx = node_base_idx + var_idx; let idx = node_base_idx + var_idx;
if test(idx).is_valid() { if test(idx).is_valid() {
wr.write_str(" "); wr.write_str(" ");
@ -743,7 +743,7 @@ class liveness {
// hack to skip the loop unless #debug is enabled: // hack to skip the loop unless #debug is enabled:
#debug["^^ liveness computation results for body %d (entry=%s)", #debug["^^ liveness computation results for body %d (entry=%s)",
{ {
uint::range(0u, self.ir.num_live_nodes) { |ln_idx| for uint::range(0u, self.ir.num_live_nodes) { |ln_idx|
#debug["%s", self.ln_str(live_node(ln_idx))]; #debug["%s", self.ln_str(live_node(ln_idx))];
} }
body.node.id body.node.id

View File

@ -945,7 +945,7 @@ fn trans_crust_fn(ccx: @crate_ctxt, path: ast_map::path, decl: ast::fn_decl,
_ { _ {
let llretptr = alloca(bcx, tys.ret_ty); let llretptr = alloca(bcx, tys.ret_ty);
let n = vec::len(tys.arg_tys); let n = vec::len(tys.arg_tys);
uint::range(0u, n) {|i| for uint::range(0u, n) {|i|
let llargval = get_param(llwrapfn, i); let llargval = get_param(llwrapfn, i);
store_inbounds(bcx, llargval, llargbundle, store_inbounds(bcx, llargval, llargbundle,
[0u, i]); [0u, i]);

View File

@ -588,7 +588,7 @@ fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
fn gen_resource_shapes(ccx: @crate_ctxt) -> ValueRef { fn gen_resource_shapes(ccx: @crate_ctxt) -> ValueRef {
let mut dtors = []; let mut dtors = [];
let len = interner::len(ccx.shape_cx.resources); let len = interner::len(ccx.shape_cx.resources);
uint::range(0u, len) {|i| for uint::range(0u, len) {|i|
let ri = interner::get(ccx.shape_cx.resources, i); let ri = interner::get(ccx.shape_cx.resources, i);
dtors += [trans::base::get_res_dtor(ccx, ri.did, ri.tps)]; dtors += [trans::base::get_res_dtor(ccx, ri.did, ri.tps)];
} }

View File

@ -73,7 +73,7 @@ fn type_uses_for(ccx: @crate_ctxt, fn_id: def_id, n_tps: uint)
} }
ast_map::node_ctor(_, _, ast_map::res_ctor(_, _, _), _) | ast_map::node_ctor(_, _, ast_map::res_ctor(_, _, _), _) |
ast_map::node_variant(_, _, _) { ast_map::node_variant(_, _, _) {
uint::range(0u, n_tps) {|n| cx.uses[n] |= use_repr;} for uint::range(0u, n_tps) {|n| cx.uses[n] |= use_repr;}
} }
ast_map::node_native_item(i@@{node: native_item_fn(_, _), _}, abi, _) { ast_map::node_native_item(i@@{node: native_item_fn(_, _), _}, abi, _) {
if abi == native_abi_rust_intrinsic { if abi == native_abi_rust_intrinsic {
@ -84,7 +84,7 @@ fn type_uses_for(ccx: @crate_ctxt, fn_id: def_id, n_tps: uint)
"get_tydesc" | "needs_drop" { use_tydesc } "get_tydesc" | "needs_drop" { use_tydesc }
"forget" | "addr_of" { 0u } "forget" | "addr_of" { 0u }
}; };
uint::range(0u, n_tps) {|n| cx.uses[n] |= flags;} for uint::range(0u, n_tps) {|n| cx.uses[n] |= flags;}
} }
} }
ast_map::node_ctor(_, _, ast_map::class_ctor(ctor, _), _){ ast_map::node_ctor(_, _, ast_map::class_ctor(ctor, _), _){

View File

@ -3,7 +3,7 @@ mod kitties {
class cat { class cat {
priv { priv {
let mut meows : uint; let mut meows : uint;
fn nap() { uint::range(1u, 10000u) {|_i|}} fn nap() { for uint::range(1u, 10000u) {|_i|}}
} }
let how_hungry : int; let how_hungry : int;

View File

@ -6,7 +6,7 @@ import io::writer_util;
fn collect_raw(num: uint) -> [uint] { fn collect_raw(num: uint) -> [uint] {
let mut result = []; let mut result = [];
uint::range(0u, num) { |i| for uint::range(0u, num) { |i|
result += [i]; result += [i];
} }
ret result; ret result;
@ -14,7 +14,7 @@ fn collect_raw(num: uint) -> [uint] {
fn collect_dvec(num: uint) -> [mut uint] { fn collect_dvec(num: uint) -> [mut uint] {
let result = dvec(); let result = dvec();
uint::range(0u, num) { |i| for uint::range(0u, num) { |i|
result.push(i); result.push(i);
} }
ret dvec::unwrap(result); ret dvec::unwrap(result);

View File

@ -38,11 +38,11 @@ fn run(args: [str]) {
let start = std::time::precise_time_s(); let start = std::time::precise_time_s();
let to_child = to_child; let to_child = to_child;
let mut worker_results = []; let mut worker_results = [];
uint::range(0u, workers) {|_i| for uint::range(0u, workers) {|_i|
let builder = task::builder(); let builder = task::builder();
worker_results += [task::future_result(builder)]; worker_results += [task::future_result(builder)];
task::run(builder) {|| task::run(builder) {||
uint::range(0u, size / workers) {|_i| for uint::range(0u, size / workers) {|_i|
comm::send(to_child, bytes(100u)); comm::send(to_child, bytes(100u));
} }
}; };

View File

@ -46,7 +46,7 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
log(debug, ">" + id + " " + desc); log(debug, ">" + id + " " + desc);
let rng = @{mut last: std::rand::rng().next()}; let rng = @{mut last: std::rand::rng().next()};
let mut op: str = ""; let mut op: str = "";
uint::range(0u, n as uint) {|_i| for uint::range(0u, n as uint) {|_i|
str::push_char(op, select_random(myrandom_next(rng, 100u32), str::push_char(op, select_random(myrandom_next(rng, 100u32),
genelist)); genelist));
if str::len(op) >= LINE_LENGTH() { if str::len(op) >= LINE_LENGTH() {
@ -61,7 +61,7 @@ fn make_repeat_fasta(id: str, desc: str, s: str, n: int) unsafe {
log(debug, ">" + id + " " + desc); log(debug, ">" + id + " " + desc);
let mut op: str = ""; let mut op: str = "";
let sl: uint = str::len(s); let sl: uint = str::len(s);
uint::range(0u, n as uint) {|i| for uint::range(0u, n as uint) {|i|
str::unsafe::push_byte(op, s[i % sl]); str::unsafe::push_byte(op, s[i % sl]);
if str::len(op) >= LINE_LENGTH() { if str::len(op) >= LINE_LENGTH() {
log(debug, op); log(debug, op);

View File

@ -69,7 +69,7 @@ fn chanmb(i: uint, size: uint, ch: comm::chan<line>) -> ()
let incr = 2./(size as f64); let incr = 2./(size as f64);
let y = incr*(i as f64) - 1.; let y = incr*(i as f64) - 1.;
let xincr = 8.*incr; let xincr = 8.*incr;
uint::range(0_u, size/8_u) { for uint::range(0_u, size/8_u) {
|j| |j|
let x = {re: xincr*(j as f64) - 1.5, im: y}; let x = {re: xincr*(j as f64) - 1.5, im: y};
crv += [fillbyte(x, incr)]; crv += [fillbyte(x, incr)];
@ -161,7 +161,7 @@ fn main(args: [str]) {
writer(path, writech, size); writer(path, writech, size);
}; };
let ch = comm::recv(writep); let ch = comm::recv(writep);
uint::range(0_u, size) {|j| for uint::range(0_u, size) {|j|
task::spawn {|| chanmb(j, size, ch);}; task::spawn {|| chanmb(j, size, ch);};
if j % yieldevery == 0_u { if j % yieldevery == 0_u {
#debug("Y %u", j); #debug("Y %u", j);

View File

@ -70,7 +70,7 @@ fn stress_task(&&id: int) {
fn stress(num_tasks: int) { fn stress(num_tasks: int) {
let mut results = []; let mut results = [];
range(0, num_tasks) {|i| for range(0, num_tasks) {|i|
let builder = task::builder(); let builder = task::builder();
results += [task::future_result(builder)]; results += [task::future_result(builder)];
task::run(builder) {|| stress_task(i); } task::run(builder) {|| stress_task(i); }
@ -99,8 +99,8 @@ fn main(args: [str]) {
let out = io::stdout(); let out = io::stdout();
range(1, max + 1) {|n| for range(1, max + 1) {|n|
range(0, num_trials) {|i| for range(0, num_trials) {|i|
let start = time::precise_time_ns(); let start = time::precise_time_ns();
let fibn = fib(n); let fibn = fib(n);
let stop = time::precise_time_ns(); let stop = time::precise_time_ns();

View File

@ -8,7 +8,7 @@ fn start(+token: int) {
let p = comm::port(); let p = comm::port();
let mut ch = comm::chan(p); let mut ch = comm::chan(p);
int::range(2, n_threads + 1) { |i| for int::range(2, n_threads + 1) { |i|
let id = n_threads + 2 - i; let id = n_threads + 2 - i;
let to_child = task::spawn_listener::<int> {|p, copy ch| let to_child = task::spawn_listener::<int> {|p, copy ch|
roundtrip(id, p, ch) roundtrip(id, p, ch)

View File

@ -6,13 +6,13 @@ import std::smallintmap::{smallintmap, map};
import io::writer_util; import io::writer_util;
fn append_sequential(min: uint, max: uint, map: smallintmap<uint>) { fn append_sequential(min: uint, max: uint, map: smallintmap<uint>) {
uint::range(min, max) { |i| for uint::range(min, max) { |i|
map.insert(i, i + 22u); map.insert(i, i + 22u);
} }
} }
fn check_sequential(min: uint, max: uint, map: smallintmap<uint>) { fn check_sequential(min: uint, max: uint, map: smallintmap<uint>) {
uint::range(min, max) { |i| for uint::range(min, max) { |i|
assert map.get(i) == i + 22u; assert map.get(i) == i + 22u;
} }
} }
@ -31,7 +31,7 @@ fn main(args: [str]) {
let mut checkf = 0.0; let mut checkf = 0.0;
let mut appendf = 0.0; let mut appendf = 0.0;
uint::range(0u, rep) {|_r| for uint::range(0u, rep) {|_r|
let map = smallintmap::mk(); let map = smallintmap::mk();
let start = std::time::precise_time_s(); let start = std::time::precise_time_s();
append_sequential(0u, max, map); append_sequential(0u, max, map);

View File

@ -51,7 +51,7 @@ fn solve_grid(g: grid_t) {
if start_color < 10u8 { if start_color < 10u8 {
// colors not yet used // colors not yet used
let avail = bitv::bitv(10u, false); let avail = bitv::bitv(10u, false);
u8::range(start_color, 10u8) { |color| for u8::range(start_color, 10u8) { |color|
bitv::set(avail, color as uint, true); bitv::set(avail, color as uint, true);
} }
@ -81,7 +81,7 @@ fn solve_grid(g: grid_t) {
let it = bind drop_color(g, avail, _, _); let it = bind drop_color(g, avail, _, _);
u8::range(0u8, 9u8) { |idx| for u8::range(0u8, 9u8) { |idx|
it(idx, col); /* check same column fields */ it(idx, col); /* check same column fields */
it(row, idx); /* check same row fields */ it(row, idx); /* check same row fields */
} }
@ -89,14 +89,14 @@ fn solve_grid(g: grid_t) {
// check same block fields // check same block fields
let row0 = (row / 3u8) * 3u8; let row0 = (row / 3u8) * 3u8;
let col0 = (col / 3u8) * 3u8; let col0 = (col / 3u8) * 3u8;
u8::range(row0, row0 + 3u8) { |alt_row| for u8::range(row0, row0 + 3u8) { |alt_row|
u8::range(col0, col0 + 3u8) { |alt_col| it(alt_row, alt_col); } for u8::range(col0, col0 + 3u8) { |alt_col| it(alt_row, alt_col); }
} }
} }
let mut work: [(u8, u8)] = []; /* queue of uncolored fields */ let mut work: [(u8, u8)] = []; /* queue of uncolored fields */
u8::range(0u8, 9u8) { |row| for u8::range(0u8, 9u8) { |row|
u8::range(0u8, 9u8) { |col| for u8::range(0u8, 9u8) { |col|
let color = (*g)[row][col]; let color = (*g)[row][col];
if color == 0u8 { work += [(row, col)]; } if color == 0u8 { work += [(row, col)]; }
} }
@ -119,9 +119,9 @@ fn solve_grid(g: grid_t) {
} }
fn write_grid(f: io::writer, g: grid_t) { fn write_grid(f: io::writer, g: grid_t) {
u8::range(0u8, 9u8) { |row| for u8::range(0u8, 9u8) { |row|
f.write_str(#fmt("%u", (*g)[row][0] as uint)); f.write_str(#fmt("%u", (*g)[row][0] as uint));
u8::range(1u8, 9u8) { |col| for u8::range(1u8, 9u8) { |col|
f.write_str(#fmt(" %u", (*g)[row][col] as uint)); f.write_str(#fmt(" %u", (*g)[row][col] as uint));
} }
f.write_char('\n'); f.write_char('\n');

View File

@ -4,7 +4,7 @@
// allocating and freeing vectors. // allocating and freeing vectors.
fn f(&&n: uint) { fn f(&&n: uint) {
uint::range(0u, n) {|i| for uint::range(0u, n) {|i|
let mut v: [u8] = []; let mut v: [u8] = [];
vec::reserve(v, 1000u); vec::reserve(v, 1000u);
} }
@ -19,5 +19,5 @@ fn main(args: [str]) {
args args
}; };
let n = uint::from_str(args[1]).get(); let n = uint::from_str(args[1]).get();
uint::range(0u, 100u) {|i| task::spawn {|| f(n); };} for uint::range(0u, 100u) {|i| task::spawn {|| f(n); };}
} }

View File

@ -65,7 +65,7 @@ fn while_aliased_mut_cond(cond: bool, cond2: bool) {
fn loop_in_block() { fn loop_in_block() {
let mut v = ~3, w = ~4; let mut v = ~3, w = ~4;
let mut _x = &mut w; let mut _x = &mut w;
uint::range(0u, 10u) {|_i| for uint::range(0u, 10u) {|_i|
borrow(v); //! ERROR loan of mutable upvar as immutable conflicts with prior loan borrow(v); //! ERROR loan of mutable upvar as immutable conflicts with prior loan
_x = &mut v; //! NOTE prior loan as mutable granted here _x = &mut v; //! NOTE prior loan as mutable granted here
} }

View File

@ -3,5 +3,5 @@ fn test(-x: uint) {}
fn main() { fn main() {
let i = 3u; let i = 3u;
uint::range(0u, 10u) {|_x| test(i)} for uint::range(0u, 10u) {|_x| test(i)}
} }

View File

@ -17,7 +17,7 @@ fn main() {
let map = map::hashmap(hash, eq); let map = map::hashmap(hash, eq);
let mut arr = []; let mut arr = [];
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
arr += [@"key stuff"]; arr += [@"key stuff"];
map.insert(arr, arr + [@"value stuff"]); map.insert(arr, arr + [@"value stuff"]);
} }

View File

@ -51,7 +51,7 @@ class cat implements noisy {
} }
fn annoy_neighbors<T: noisy>(critter: T) { fn annoy_neighbors<T: noisy>(critter: T) {
uint::range(0u, 10u) {|i| critter.speak(); } for uint::range(0u, 10u) {|i| critter.speak(); }
} }
fn main() { fn main() {

View File

@ -61,7 +61,7 @@ class cat implements map<int, bool> {
fn main() { fn main() {
let nyan : cat = cat(0, 2, "nyan"); let nyan : cat = cat(0, 2, "nyan");
uint::range(1u, 5u) {|_i| nyan.speak(); } for uint::range(1u, 5u) {|_i| nyan.speak(); }
// cat returns true if uint input is greater than // cat returns true if uint input is greater than
// the number of meows so far // the number of meows so far
assert(nyan.get(1)); assert(nyan.get(1));

View File

@ -40,6 +40,6 @@ fn main() {
let nyan = cat(0u, 2, "nyan"); let nyan = cat(0u, 2, "nyan");
nyan.eat(); nyan.eat();
assert(!nyan.eat()); assert(!nyan.eat());
uint::range(1u, 10u, {|_i| nyan.speak(); }); for uint::range(1u, 10u) {|_i| nyan.speak(); };
assert(nyan.eat()); assert(nyan.eat());
} }

View File

@ -43,6 +43,6 @@ fn main() {
let nyan = cat(0u, 2, "nyan"); let nyan = cat(0u, 2, "nyan");
nyan.eat(); nyan.eat();
assert(!nyan.eat()); assert(!nyan.eat());
uint::range(1u, 10u, {|_i| make_speak(nyan); }); for uint::range(1u, 10u) {|_i| make_speak(nyan); };
assert(nyan.eat()); assert(nyan.eat());
} }

View File

@ -84,7 +84,7 @@ class cat implements noisy, scratchy, bitey {
} }
fn annoy_neighbors<T: noisy>(critter: T) { fn annoy_neighbors<T: noisy>(critter: T) {
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
let what = critter.speak(); let what = critter.speak();
#debug("%u %d", i, what); #debug("%u %d", i, what);
} }

View File

@ -7,6 +7,6 @@ fn main() {
let nyan = cat(0u, 2, "nyan"); let nyan = cat(0u, 2, "nyan");
nyan.eat(); nyan.eat();
assert(!nyan.eat()); assert(!nyan.eat());
uint::range(1u, 10u, {|_i| nyan.speak(); }); for uint::range(1u, 10u) {|_i| nyan.speak(); };
assert(nyan.eat()); assert(nyan.eat());
} }

View File

@ -35,6 +35,6 @@ fn main() {
let nyan = cat(0u, 2, "nyan"); let nyan = cat(0u, 2, "nyan");
nyan.eat(); nyan.eat();
assert(!nyan.eat()); assert(!nyan.eat());
uint::range(1u, 10u, {|_i| nyan.speak(); }); for uint::range(1u, 10u) {|_i| nyan.speak(); };
assert(nyan.eat()); assert(nyan.eat());
} }

View File

@ -28,8 +28,16 @@ fn foldl<A,B,IA:iterable<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
ret b; ret b;
} }
fn range(lo: uint, hi: uint, it: fn(uint)) {
let mut i = lo;
while i < hi {
it(i);
i += 1u;
}
}
fn main() { fn main() {
let range = bind uint::range(0u, 1000u, _); let range = bind range(0u, 1000u, _);
let filt = bind filter( let filt = bind filter(
range, range,
{|&&n: uint| n % 3u != 0u && n % 5u != 0u }, {|&&n: uint| n % 3u != 0u && n % 5u != 0u },

View File

@ -13,7 +13,7 @@ fn iloop() {
} }
fn main() { fn main() {
uint::range(0u, 100u) {|_i| for uint::range(0u, 100u) {|_i|
let builder = task::builder(); let builder = task::builder();
task::unsupervise(builder); task::unsupervise(builder);
task::run(builder) {|| iloop(); }; task::run(builder) {|| iloop(); };

View File

@ -10,7 +10,7 @@ fn test(x: bool, foo: ~{x: int, y: int, z: int}) -> int {
fn main() { fn main() {
let x = ~{x: 1, y: 2, z: 3}; let x = ~{x: 1, y: 2, z: 3};
uint::range(0u, 10000u) {|_i| for uint::range(0u, 10000u) {|_i|
assert (test(true, x) == 2); assert (test(true, x) == 2);
} }
assert (test(false, x) == 5); assert (test(false, x) == 5);

View File

@ -10,7 +10,7 @@ fn test(x: bool, foo: @{x: int, y: int, z: int}) -> int {
fn main() { fn main() {
let x = @{x: 1, y: 2, z: 3}; let x = @{x: 1, y: 2, z: 3};
uint::range(0u, 10000u) {|i| for uint::range(0u, 10000u) {|i|
assert (test(true, x) == 2); assert (test(true, x) == 2);
} }
assert (test(false, x) == 5); assert (test(false, x) == 5);

View File

@ -6,7 +6,7 @@ fn foo(src: uint) {
alt some(src) { alt some(src) {
some(src_id) { some(src_id) {
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
let yyy = src_id; let yyy = src_id;
assert (yyy == 0u); assert (yyy == 0u);
} }

View File

@ -1,7 +1,7 @@
class cat { class cat {
priv { priv {
let mut meows : uint; let mut meows : uint;
fn nap() { uint::range(1u, 10u) {|_i|}} fn nap() { for uint::range(1u, 10u) {|_i|}}
} }
let how_hungry : int; let how_hungry : int;

View File

@ -32,7 +32,7 @@ fn test_init() {
fn test_grow() { fn test_grow() {
let myport = port(); let myport = port();
let mychan = chan(myport); let mychan = chan(myport);
uint::range(0u, 100u) {|i| for uint::range(0u, 100u) {|i|
let val: record = {val1: 0u32, val2: 0u32, val3: 0u32}; let val: record = {val1: 0u32, val2: 0u32, val3: 0u32};
comm::send(mychan, val); comm::send(mychan, val);
} }
@ -50,11 +50,11 @@ fn test_shrink1() {
fn test_shrink2() { fn test_shrink2() {
let myport = port(); let myport = port();
let mychan = chan(myport); let mychan = chan(myport);
uint::range(0u, 100u) {|_i| for uint::range(0u, 100u) {|_i|
let val: record = {val1: 0u32, val2: 0u32, val3: 0u32}; let val: record = {val1: 0u32, val2: 0u32, val3: 0u32};
send(mychan, val); send(mychan, val);
} }
uint::range(0u, 100u) {|_i| let x = recv(myport); } for uint::range(0u, 100u) {|_i| let x = recv(myport); }
} }
@ -62,7 +62,7 @@ fn test_shrink2() {
fn test_rotate() { fn test_rotate() {
let myport = port(); let myport = port();
let mychan = chan(myport); let mychan = chan(myport);
uint::range(0u, 100u) {|i| for uint::range(0u, 100u) {|i|
let val = {val1: i as u32, val2: i as u32, val3: i as u32}; let val = {val1: i as u32, val2: i as u32, val3: i as u32};
send(mychan, val); send(mychan, val);
let x = recv(myport); let x = recv(myport);
@ -78,13 +78,13 @@ fn test_rotate() {
fn test_rotate_grow() { fn test_rotate_grow() {
let myport = port::<record>(); let myport = port::<record>();
let mychan = chan(myport); let mychan = chan(myport);
uint::range(0u, 10u) {|j| for uint::range(0u, 10u) {|j|
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
let val: record = let val: record =
{val1: i as u32, val2: i as u32, val3: i as u32}; {val1: i as u32, val2: i as u32, val3: i as u32};
send(mychan, val); send(mychan, val);
} }
uint::range(0u, 10u) {|i| for uint::range(0u, 10u) {|i|
let x = recv(myport); let x = recv(myport);
assert (x.val1 == i as u32); assert (x.val1 == i as u32);
assert (x.val2 == i as u32); assert (x.val2 == i as u32);

View File

@ -21,7 +21,7 @@ fn iloop() {
} }
fn main() { fn main() {
uint::range(0u, 16u) {|_i| for uint::range(0u, 16u) {|_i|
let builder = task::builder(); let builder = task::builder();
task::unsupervise(builder); task::unsupervise(builder);
task::run(builder) {|| iloop(); } task::run(builder) {|| iloop(); }

View File

@ -7,13 +7,13 @@ import task;
// results in the string not being freed // results in the string not being freed
fn starship(&&ch: comm::chan<str>) { fn starship(&&ch: comm::chan<str>) {
int::range(0, 10) { |_i| for int::range(0, 10) { |_i|
comm::send(ch, "pew pew"); comm::send(ch, "pew pew");
} }
} }
fn starbase() { fn starbase() {
int::range(0, 10) { |_i| for int::range(0, 10) { |_i|
let p = comm::port(); let p = comm::port();
let c = comm::chan(p); let c = comm::chan(p);
task::spawn {|| starship(c);}; task::spawn {|| starship(c);};
@ -22,7 +22,7 @@ fn starbase() {
} }
fn main() { fn main() {
int::range(0, 10) { |_i| for int::range(0, 10) { |_i|
task::spawn {|| starbase();}; task::spawn {|| starbase();};
} }
} }

View File

@ -12,13 +12,13 @@ fn main() {
let ch = comm::chan(p); let ch = comm::chan(p);
let n = 100u; let n = 100u;
let mut expected = 0u; let mut expected = 0u;
uint::range(0u, n) {|i| for uint::range(0u, n) {|i|
task::spawn {|| child(ch, i); }; task::spawn {|| child(ch, i); };
expected += i; expected += i;
} }
let mut actual = 0u; let mut actual = 0u;
uint::range(0u, n) {|_i| for uint::range(0u, n) {|_i|
let j = comm::recv(p); let j = comm::recv(p);
actual += *j; actual += *j;
} }