Remove remaining uses of iter and for-each

Issue #1056
This commit is contained in:
Marijn Haverbeke 2011-10-21 13:14:28 +02:00
parent fd5a08384d
commit 050170d2af
41 changed files with 125 additions and 222 deletions

View File

@ -4904,14 +4904,14 @@ fn trans_fn_cleanups(fcx: @fn_ctxt, cx: @block_ctxt) {
}
}
iter block_locals(b: ast::blk) -> @ast::local {
fn block_locals(b: ast::blk, it: block(@ast::local)) {
for s: @ast::stmt in b.node.stmts {
alt s.node {
ast::stmt_decl(d, _) {
alt d.node {
ast::decl_local(locals) {
for (style, local) in locals {
if style == ast::let_copy { put local; }
if style == ast::let_copy { it(local); }
}
}
_ {/* fall through */ }
@ -5015,9 +5015,7 @@ fn trans_block(bcx: @block_ctxt, b: ast::blk) -> @block_ctxt {
fn trans_block_dps(bcx: @block_ctxt, b: ast::blk, dest: dest)
-> @block_ctxt {
for each local: @ast::local in block_locals(b) {
bcx = alloc_local(bcx, local);
}
block_locals(b) {|local| bcx = alloc_local(bcx, local); };
for s: @ast::stmt in b.node.stmts {
bcx = trans_stmt(bcx, *s);
}

View File

@ -24,11 +24,11 @@ fn load_props(testfile: str) -> test_props {
let error_patterns = [];
let compile_flags = option::none;
let pp_exact = option::none;
for each ln: str in iter_header(testfile) {
iter_header(testfile) {|ln|
alt parse_error_pattern(ln) {
option::some(ep) { error_patterns += [ep]; }
option::none. { }
}
};
if option::is_none(compile_flags) {
compile_flags = parse_compile_flags(ln);
@ -37,7 +37,7 @@ fn load_props(testfile: str) -> test_props {
if option::is_none(pp_exact) {
pp_exact = parse_pp_exact(ln, testfile);
}
}
};
ret {
error_patterns: error_patterns,
compile_flags: compile_flags,
@ -47,14 +47,14 @@ fn load_props(testfile: str) -> test_props {
fn is_test_ignored(config: config, testfile: str) -> bool {
let found = false;
for each ln: str in iter_header(testfile) {
iter_header(testfile) {|ln|
// FIXME: Can't return or break from iterator
found = found || parse_name_directive(ln, "xfail-test");
found = found || parse_name_directive(ln, xfail_target());
if (config.mode == common::mode_pretty) {
found = found || parse_name_directive(ln, "xfail-pretty");
}
}
};
ret found;
fn xfail_target() -> str {
@ -62,7 +62,7 @@ fn is_test_ignored(config: config, testfile: str) -> bool {
}
}
iter iter_header(testfile: str) -> str {
fn iter_header(testfile: str, it: block(str)) {
let rdr = io::file_reader(testfile);
while !rdr.eof() {
let ln = rdr.read_line();
@ -73,7 +73,7 @@ iter iter_header(testfile: str) -> str {
if str::starts_with(ln, "fn")
|| str::starts_with(ln, "mod") {
break;
} else { put ln; }
} else { it(ln); }
}
}

View File

@ -23,9 +23,9 @@ type pointy = {
mutable z : fn()->()
};
iter allunder(n: uint) -> uint {
fn allunder(n: uint, it: block(uint)) {
let i: uint = 0u;
while i < n { put i; i += 1u; }
while i < n { it(i); i += 1u; }
}
fn nopT(_x : @pointy) { }
@ -36,20 +36,20 @@ fn test_cycles(r : rand::rng)
const max : uint = 10u;
let v : [mutable @pointy] = [mutable];
for each i in allunder(max) {
allunder(max) {|i|
v += [mutable @{ mutable x : no_pointy, mutable y : no_pointy, mutable z: nop }];
}
};
for each i in allunder(max) {
allunder(max) {|i|
v[i].x = yes_pointy(v[under(r, max)]);
v[i].y = yes_pointy(v[under(r, max)]);
v[i].z = bind nopT(v[under(r, max)]);
}
};
// Drop refs one at a time
for each i in allunder(max) {
allunder(max) {|i|
v[i] = @{ mutable x : no_pointy, mutable y : no_pointy, mutable z: nop };
}
};
}
fn main()

View File

@ -209,9 +209,9 @@ fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::ty, tm: test_mode
*crate2
}
iter under(n: uint) -> uint {
fn under(n: uint, it: block(uint)) {
let i: uint = 0u;
while i < n { put i; i += 1u; }
while i < n { it(i); i += 1u; }
}
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
@ -245,9 +245,9 @@ fn check_variants_T<@T>(
let L = vec::len(things);
if L < 100u {
for each i: uint in under(uint::min(L, 20u)) {
under(uint::min(L, 20u)) {|i|
log_err "Replacing... #" + uint::str(i);
for each j: uint in under(uint::min(L, 30u)) {
under(uint::min(L, 30u)) {|j|
log_err "With... " + stringifier(@things[j]);
let crate2 = @replacer(crate, i, things[j], cx.mode);
// It would be best to test the *crate* for stability, but testing the
@ -267,8 +267,8 @@ fn check_variants_T<@T>(
check_whole_compiler(str3, file_label, safe_to_run);
}
}
}
}
};
};
}
}

View File

@ -42,9 +42,9 @@ fn vec_insert<@T>(v: [T], i: uint, x: T) -> [T] {
}
// Iterates over 0...length, skipping the specified number on each side.
iter ix(skip_low: uint, skip_high: uint, length: uint) -> uint {
fn ix(skip_low: uint, skip_high: uint, length: uint, it: block(uint)) {
let i: uint = skip_low;
while i + skip_high <= length { put i; i += 1u; }
while i + skip_high <= length { it(i); i += 1u; }
}
// Returns a bunch of modified versions of v, some of which introduce new elements (borrowed from xs).
@ -60,20 +60,20 @@ fn vec_edits<@T>(v: [T], xs: [T]) -> [[T]] {
// When Lv == 2u, this is redundant with swap.
edits += [vec::reversed(v)];
}
for each i: uint in ix(0u, 1u, Lv) { edits += [vec_omit(v, i)]; }
for each i: uint in ix(0u, 1u, Lv) { edits += [vec_dup(v, i)]; }
for each i: uint in ix(0u, 2u, Lv) { edits += [vec_swadj(v, i)]; }
for each i: uint in ix(1u, 2u, Lv) { edits += [vec_prefix(v, i)]; }
for each i: uint in ix(2u, 1u, Lv) { edits += [vec_suffix(v, i)]; }
ix(0u, 1u, Lv) {|i| edits += [vec_omit(v, i)]; };
ix(0u, 1u, Lv) {|i| edits += [vec_dup(v, i)]; };
ix(0u, 2u, Lv) {|i| edits += [vec_swadj(v, i)]; };
ix(1u, 2u, Lv) {|i| edits += [vec_prefix(v, i)]; };
ix(2u, 1u, Lv) {|i| edits += [vec_suffix(v, i)]; };
for each j: uint in ix(0u, 1u, len(xs)) {
for each i: uint in ix(0u, 1u, Lv) {
ix(0u, 1u, len(xs)) {|j|
ix(0u, 1u, Lv) {|i|
edits += [vec_poke(v, i, xs[j])];
}
for each i: uint in ix(0u, 0u, Lv) {
};
ix(0u, 0u, Lv) {|i|
edits += [vec_insert(v, i, xs[j])];
}
}
};
};
edits
}
@ -93,7 +93,7 @@ fn vec_to_str(v: [int]) -> str {
fn show_edits(a: [int], xs: [int]) {
log_err "=== Edits of " + vec_to_str(a) + " ===";
let b = vec_edits(a, xs);
for each i: uint in ix(0u, 1u, len(b)) { log_err vec_to_str(b[i]); }
ix(0u, 1u, len(b)) {|i| log_err vec_to_str(b[i]); };
}
fn demo_edits() {

View File

@ -40,14 +40,17 @@ fn sub(t: str, n: int) -> str {
/* Using an interator */
iter ninetynine() -> int { let n: int = 100; while n > 1 { n -= 1; put n; } }
fn ninetynine(it: block(int)) {
let n: int = 100;
while n > 1 { n -= 1; it(n); }
}
fn main() {
for each n: int in ninetynine() {
ninetynine {|n|
log sub(b1(), n);
log sub(b2(), n - 1);
log "";
}
};
log b7();
log b8();
}

View File

@ -47,10 +47,10 @@ fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
log ">" + id + " " + desc;
let rng = myrandom(std::rand::mk_rng().next());
let op: str = "";
for each i: uint in uint::range(0u, n as uint) {
uint::range(0u, n as uint) {|i|
str::push_byte(op, select_random(rng.next(100u32), genelist) as u8);
if str::byte_len(op) >= LINE_LENGTH() { log op; op = ""; }
}
};
if str::byte_len(op) > 0u { log op; }
}
@ -58,10 +58,10 @@ fn make_repeat_fasta(id: str, desc: str, s: str, n: int) {
log ">" + id + " " + desc;
let op: str = "";
let sl: uint = str::byte_len(s);
for each i: uint in uint::range(0u, n as uint) {
uint::range(0u, n as uint) {|i|
str::push_byte(op, s[i % sl]);
if str::byte_len(op) >= LINE_LENGTH() { log op; op = ""; }
}
};
if str::byte_len(op) > 0u { log op; }
}

View File

@ -1,4 +1,5 @@
// -*- rust -*-
// xfail-pretty
/*
A parallel version of fibonacci numbers.
@ -74,9 +75,9 @@ fn stress_task(&&id: int) {
fn stress(num_tasks: int) {
let tasks = [];
for each i: int in range(0, num_tasks) {
range(0, num_tasks) {|i|
tasks += [task::spawn_joinable(copy i, stress_task)];
}
};
for t in tasks { task::join(t); }
}
@ -98,8 +99,8 @@ fn main(argv: [str]) {
let out = io::stdout();
for each n: int in range(1, max + 1) {
for each i: int in range(0, num_trials) {
range(1, max + 1) {|n|
range(0, num_trials) {|i|
let start = time::precise_time_ns();
let fibn = fib(n);
let stop = time::precise_time_ns();
@ -108,8 +109,8 @@ fn main(argv: [str]) {
out.write_line(#fmt["%d\t%d\t%s", n, fibn,
u64::str(elapsed)]);
}
}
};
};
}
}
}

View File

@ -10,10 +10,10 @@ import std::str;
import std::task;
fn f(&&n: uint) {
for each i in uint::range(0u, n) {
uint::range(0u, n) {|i|
let v: [u8] = [];
vec::reserve(v, 1000u);
}
};
}
fn main(args: [str]) {
@ -21,5 +21,5 @@ fn main(args: [str]) {
if vec::len(args) < 2u {
100u
} else { uint::parse_buf(str::bytes(args[1]), 10u) };
for each i in uint::range(0u, 100u) { task::spawn(copy n, f); }
uint::range(0u, 100u) {|i| task::spawn(copy n, f); };
}

View File

@ -100,10 +100,7 @@ mod map_reduce {
map(input, bind emit(intermediates, ctrl, _, _));
for each kv: @{key: str, val: chan<reduce_proto>} in
intermediates.items() {
send(kv.val, release);
}
intermediates.values {|v| send(v, release); };
send(ctrl, mapper_done);
}
@ -181,10 +178,7 @@ mod map_reduce {
}
}
for each kv: @{key: str, val: chan<reduce_proto>} in reducers.items()
{
send(kv.val, done);
}
reducers.values {|v| send(v, done); };
for t in tasks { task::join(t); }
}

View File

@ -1,3 +0,0 @@
// error-pattern:calling non-iter as sequence of for each loop
fn f() -> int { ret 4; }
fn main() { for each i in f() { } }

View File

@ -1,2 +0,0 @@
// error-pattern:sequence in for each loop not a call
fn main() { for each p in 1 { } }

View File

@ -10,9 +10,9 @@ fn bitv_to_str(enclosing: fn_info, v: bitv::t) -> str {
let s = "";
// error is that the value type in the hash map is var_info, not a box
for each p: @{key: uint, val: @uint} in enclosing.vars.items() {
if bitv::get(v, *p.val) { s += "foo"; }
}
enclosing.vars.values {|val|
if bitv::get(v, val) { s += "foo"; }
};
ret s;
}

View File

@ -1,3 +0,0 @@
// error-pattern:calling iter outside of for each loop
iter i() { }
fn main() { i(); }

View File

@ -1,5 +0,0 @@
// error-pattern:put in non-iterator
fn f() -> int { put 10; }
fn main() { }

View File

@ -1,4 +1,4 @@
// error-pattern:moop
use std;
import std::uint;
fn main() { for each i: uint in uint::range(0u, 10u) { fail "moop"; } }
fn main() { uint::range(0u, 10u) {|_i| fail "moop"; } }

View File

@ -1,12 +1,11 @@
// error-pattern:fail
iter x() -> int {
fn x(it: block(int)) {
fail;
put 0;
it(0);
}
fn main() {
let a = @0;
for each x in x() {
}
x {|_i|};
}

View File

@ -1,12 +1,10 @@
// error-pattern:fail
iter x() -> int {
fn x(it: block(int)) {
let a = @0;
put 1;
it(1);
}
fn main() {
for each x in x() {
fail;
}
x {|_x| fail; };
}

View File

@ -21,8 +21,8 @@ fn main() {
let map = map::mk_hashmap(hash, eq);
let arr = [];
for each i in uint::range(0u, 10u) {
uint::range(0u, 10u) {|i|
arr += [@"key stuff"];
map.insert(arr, arr + [@"value stuff"]);
}
};
}

View File

@ -2,12 +2,12 @@
tag thing { a; b; c; }
iter foo() -> int { put 10; }
fn foo(it: block(int)) { it(10); }
fn main() {
let x = true;
alt a {
a. { x = true; for each i: int in foo() { } }
a. { x = true; foo {|_i|} }
b. { x = false; }
c. { x = false; }
}

View File

@ -1,8 +0,0 @@
iter x() -> {x: int, y: int} {
let i = 0;
while i < 40 { put {x: i, y: 30 - i}; i += 10; }
}
fn main() {
for each {x: x, y: y}: {x: int, y: int} in x() { assert (x + y == 30); }
}

View File

@ -1,9 +0,0 @@
obj ob<@K>(k: K) {
iter foo() -> @{a: K} { put @{a: k}; }
}
fn x(o: ob<str>) { for each i: @{a: str} in o.foo() { } }
fn main() { let o = ob::<str>("hi" + "there"); x(o); }

View File

@ -1,29 +0,0 @@
// -*- rust -*-
iter two() -> int { put 0; put 1; }
iter range(start: int, stop: int) -> int {
let i: int = start;
while i < stop { put i; i += 1; }
}
fn main() {
let a: [mutable int] = [mutable -1, -1, -1, -1, -1, -1, -1, -1];
let p: int = 0;
for each i: int in two() {
for each j: int in range(0, 2) {
let tmp: int = 10 * i + j;
for each k: int in range(0, 2) { a[p] = 10 * tmp + k; p += 1; }
}
}
assert (a[0] == 0);
assert (a[1] == 1);
assert (a[2] == 10);
assert (a[3] == 11);
assert (a[4] == 100);
assert (a[5] == 101);
assert (a[6] == 110);
assert (a[7] == 111);
}

View File

@ -2,14 +2,14 @@
// -*- rust -*-
iter two() -> int { put 0; put 1; }
fn two(it: block(int)) { it(0); it(1); }
fn main() {
let a: [mutable int] = [mutable -1, -1, -1, -1];
let p: int = 0;
for each i: int in two() {
for each j: int in two() { a[p] = 10 * i + j; p += 1; }
}
two {|i|
two {|j| a[p] = 10 * i + j; p += 1; };
};
assert (a[0] == 0);
assert (a[1] == 1);
assert (a[2] == 10);

View File

@ -1,20 +1,21 @@
iter pairs() -> {_0: int, _1: int} {
fn pairs(it: block((int, int))) {
let i: int = 0;
let j: int = 0;
while i < 10 { put {_0: i, _1: j}; i += 1; j += i; }
while i < 10 { it((i, j)); i += 1; j += i; }
}
fn main() {
let i: int = 10;
let j: int = 0;
for each p: {_0: int, _1: int} in pairs() {
log p._0;
log p._1;
assert (p._0 + 10 == i);
pairs() {|p|
let (_0, _1) = p;
log _0;
log _1;
assert (_0 + 10 == i);
i += 1;
j = p._1;
}
j = _1;
};
assert (j == 45);
}

View File

@ -4,13 +4,13 @@
// -*- rust -*-
fn main() {
let sum: int = 0;
for each i: int in first_ten() { log "main"; log i; sum = sum + i; }
first_ten {|i| log "main"; log i; sum = sum + i; };
log "sum";
log sum;
assert (sum == 45);
}
iter first_ten() -> int {
fn first_ten(it: block(int)) {
let i: int = 0;
while i < 10 { log "first_ten"; put i; i = i + 1; }
while i < 10 { log "first_ten"; it(i); i = i + 1; }
}

View File

@ -1,10 +0,0 @@
// -*- rust -*-
fn main() { for each i: int in first_ten() { log "main"; } }
iter first_ten() -> int {
let i: int = 90;
while i < 100 { log "first_ten"; log i; put i; i = i + 1; }
}

View File

@ -1,8 +1,8 @@
obj ob<@K>(k: K) {
iter foo() -> ~{a: K} { put ~{a: k}; }
fn foo(it: block(~{a: K})) { it(~{a: k}); }
}
fn x(o: ob<str>) { for each i: ~{a: str} in o.foo() { } }
fn x(o: ob<str>) { o.foo() {|_i|}; }
fn main() { let o = ob::<str>("hi" + "there"); x(o); }

View File

@ -1,9 +0,0 @@
// Contrived example? No. It showed up in rustc's resolve pass.
iter i() { put (); }
fn foo<T>(t: T) { let x: int = 10; for each j: () in i() { log x; } }
fn main() { foo(0xdeadbeef_u); }

View File

@ -1,13 +1,13 @@
iter range(a: int, b: int) -> int {
fn range(a: int, b: int, it: block(int)) {
assert (a < b);
let i: int = a;
while i < b { put i; i += 1; }
while i < b { it(i); i += 1; }
}
fn main() {
let sum: int = 0;
for each x: int in range(0, 100) { sum += x; }
range(0, 100) {|x| sum += x; };
log sum;
}

View File

@ -1,7 +0,0 @@
iter x() -> int { }
fn f() -> bool { for each i: int in x() { ret true; } ret false; }
fn main(args: [str]) { f(); }

View File

@ -14,7 +14,7 @@ fn iloop(&&_i: ()) {
}
fn main() {
for each i in uint::range(0u, 100u) {
uint::range(0u, 100u) {|_i|
task::spawn((), iloop);
}
};
}

View File

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

View File

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

View File

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

View File

@ -1,2 +0,0 @@
iter iter2<@T>() -> T { }
fn main() { for each i: int in iter2() { } }

View File

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

View File

@ -19,7 +19,7 @@ fn iloop(&&_i: ()) {
}
fn main() {
for each i in uint::range(0u, 16u) {
uint::range(0u, 16u) {|_i|
task::spawn((), iloop);
}
};
}

View File

@ -1,12 +1,12 @@
iter range(lo: uint, hi: uint) -> uint {
fn range(lo: uint, hi: uint, it: block(uint)) {
let lo_ = lo;
while lo_ < hi { put lo_; lo_ += 1u; }
while lo_ < hi { it(lo_); lo_ += 1u; }
}
fn create_index<@T>(index: [{a: T, b: uint}], hash_fn: fn(T) -> uint) {
for each i: uint in range(0u, 256u) { let bucket: [T] = []; }
range(0u, 256u) {|_i| let bucket: [T] = []; };
}
fn main() { }

View File

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

View File

@ -17,10 +17,6 @@ fn bind_id_1() { bind id(fail); }
fn bind_id_2() { bind id(ret); }
iter put_break() -> int {
while true { put break; }
}
fn fail_fail() { fail fail; }
fn log_fail() { log_err fail; }