Permalink
Browse files

Remove remaining uses of iter and for-each

Issue #1056
  • Loading branch information...
1 parent fd5a083 commit 050170d2af94ea81b9b964901e4de7eb79583332 @marijnh marijnh committed Oct 21, 2011
Showing with 126 additions and 223 deletions.
  1. +3 −5 src/comp/middle/trans.rs
  2. +7 −7 src/compiletest/header.rs
  3. +8 −8 src/fuzzer/cycles.rs
  4. +6 −6 src/fuzzer/fuzzer.rs
  5. +15 −15 src/fuzzer/ivec_fuzz.rs
  6. +6 −3 src/test/bench/99bob-iter.rs
  7. +4 −4 src/test/bench/shootout-fasta.rs
  8. +7 −6 src/test/bench/shootout-pfib.rs
  9. +3 −3 src/test/bench/task-perf-vector-party.rs
  10. +2 −8 src/test/bench/task-perf-word-count.rs
  11. +0 −3 src/test/compile-fail/fn-as-iter.rs
  12. +0 −2 src/test/compile-fail/for-each-over-bs.rs
  13. +3 −3 src/test/compile-fail/for-loop-decl.rs
  14. +0 −3 src/test/compile-fail/iter-as-fn.rs
  15. +0 −5 src/test/compile-fail/put-in-fn.rs
  16. +1 −1 src/test/run-fail/for-each-loop-fail.rs
  17. +3 −4 src/test/run-fail/unwind-iter.rs
  18. +3 −5 src/test/run-fail/unwind-iter2.rs
  19. +2 −2 src/test/run-fail/unwind-misc-1.rs
  20. +2 −2 src/test/run-pass/alt-phi.rs
  21. +0 −8 src/test/run-pass/for-each-destruct.rs
  22. +0 −9 src/test/run-pass/foreach-box-drop.rs
  23. +0 −29 src/test/run-pass/foreach-nested-2.rs
  24. +4 −4 src/test/run-pass/foreach-nested.rs
  25. +9 −8 src/test/run-pass/foreach-put-structured.rs
  26. +3 −3 src/test/run-pass/foreach-simple-outer-slot.rs
  27. +0 −10 src/test/run-pass/foreach-simple.rs
  28. +2 −2 src/test/run-pass/foreach-unique-drop.rs
  29. +0 −9 src/test/run-pass/generic-iter-frame.rs
  30. +3 −3 src/test/run-pass/iter-range.rs
  31. +0 −7 src/test/run-pass/iter-ret.rs
  32. +2 −2 src/test/run-pass/lots-a-fail.rs
  33. +2 −2 src/test/run-pass/move-3-unique.rs
  34. +2 −2 src/test/run-pass/move-3.rs
  35. +2 −2 src/test/run-pass/pattern-bound-var-in-for-each.rs
  36. +0 −2 src/test/run-pass/polymorphic-iter.rs
  37. +13 −13 src/test/run-pass/rt-circular-buffer.rs
  38. +2 −2 src/test/run-pass/send-iloop.rs
  39. +3 −3 src/test/run-pass/type-params-in-for-each.rs
  40. +4 −4 src/test/run-pass/unique-send-2.rs
  41. +0 −4 src/test/run-pass/unreachable-code.rs
@@ -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);
}
@@ -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,22 +47,22 @@ 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 {
"xfail-" + std::os::target_os()
}
}
-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
@@ -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
@@ -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);
}
}
- }
- }
+ };
+ };
}
}
@@ -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)]; }
-
- for each j: uint in ix(0u, 1u, len(xs)) {
- for each i: uint in ix(0u, 1u, Lv) {
+ 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)]; };
+
+ 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() {
@@ -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();
}
@@ -47,21 +47,21 @@ 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; }
}
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; }
}
@@ -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)]);
- }
- }
+ };
+ };
}
}
}
@@ -10,16 +10,16 @@ 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]) {
let n =
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); };
}
@@ -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); }
}
@@ -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() { } }
@@ -1,2 +0,0 @@
-// error-pattern:sequence in for each loop not a call
-fn main() { for each p in 1 { } }
@@ -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;
}
@@ -1,3 +0,0 @@
-// error-pattern:calling iter outside of for each loop
-iter i() { }
-fn main() { i(); }
@@ -1,5 +0,0 @@
-// error-pattern:put in non-iterator
-
-fn f() -> int { put 10; }
-
-fn main() { }
@@ -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"; } }
@@ -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|};
}
Oops, something went wrong.

0 comments on commit 050170d

Please sign in to comment.