auto merge of #7373 : thestinger/rust/iterator, r=huonw

This commit is contained in:
bors 2013-06-25 15:22:55 -07:00
commit efd1438770
58 changed files with 259 additions and 382 deletions

View file

@ -86,7 +86,7 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
HashSet::new()
};
for vec::each(edges) |e| {
for edges.iter().advance |e| {
match *e {
(i, j) => {
graph[i].insert(j);
@ -441,7 +441,7 @@ fn main() {
let stop = time::precise_time_s();
let mut total_edges = 0;
vec::each(graph, |edges| { total_edges += edges.len(); true });
for graph.iter().advance |edges| { total_edges += edges.len(); }
io::stdout().write_line(fmt!("Generated graph with %? edges in %? seconds.",
total_edges / 2,

View file

@ -83,7 +83,7 @@ fn run(args: &[~str]) {
server(&from_parent, &to_parent);
}
for vec::each(worker_results) |r| {
for worker_results.iter().advance |r| {
r.recv();
}

View file

@ -79,7 +79,7 @@ fn run(args: &[~str]) {
server(&from_parent, &to_parent);
}
for vec::each(worker_results) |r| {
for worker_results.iter().advance |r| {
r.recv();
}

View file

@ -188,7 +188,7 @@ fn rendezvous(nn: uint, set: ~[color]) {
// save each creature's meeting stats
let mut report = ~[];
for vec::each(to_creature) |_to_one| {
for to_creature.iter().advance |_to_one| {
report.push(from_creatures_log.recv());
}
@ -196,7 +196,7 @@ fn rendezvous(nn: uint, set: ~[color]) {
io::println(show_color_list(set));
// print each creature's stats
for vec::each(report) |rep| {
for report.iter().advance |rep| {
io::println(*rep);
}

View file

@ -56,7 +56,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
let mut pairs = ~[];
// map -> [(k,%)]
for mm.each |&key, &val| {
for mm.iter().advance |(&key, &val)| {
pairs.push((key, pct(val, total)));
}

View file

@ -8,10 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::vec;
fn main() {
for vec::each(~[0]) |_i| { //~ ERROR A for-loop body must return (), but
for 2.times { //~ ERROR A for-loop body must return (), but
true
}
}

View file

@ -16,7 +16,7 @@ struct Foo {
impl Foo {
pub fn foo(&mut self, fun: &fn(&int)) {
for self.n.each |f| {
for self.n.iter().advance |f| {
fun(f);
}
}

View file

@ -8,10 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::vec;
fn main() {
for vec::each(fail!()) |i| {
let _ = i * 2; //~ ERROR the type of this value must be known
};
let x = fail!();
x.clone(); //~ ERROR the type of this value must be known in this context
}

View file

@ -12,7 +12,7 @@ use std::vec;
fn main() {
let a: ~[int] = ~[];
vec::each(a, |_| -> bool {
a.iter().advance(|_| -> bool {
//~^ ERROR mismatched types
});
}

View file

@ -12,21 +12,19 @@
// making method calls, but only if there aren't any matches without
// it.
use std::vec;
trait iterable<A> {
fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool;
}
impl<'self,A> iterable<A> for &'self [A] {
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
vec::each(*self, f)
self.iter().advance(f)
}
}
impl<A> iterable<A> for ~[A] {
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
vec::each(*self, f)
self.iter().advance(f)
}
}

View file

@ -8,11 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::vec;
pub fn main() {
let mut sum = 0;
for vec::each(~[1, 2, 3, 4, 5]) |x| {
let xs = ~[1, 2, 3, 4, 5];
for xs.iter().advance |x| {
sum += *x;
}
assert_eq!(sum, 15);

View file

@ -15,7 +15,7 @@ pub fn main() {
let v = ~[-1f, 0f, 1f, 2f, 3f];
// Statement form does not require parentheses:
for vec::each(v) |i| {
for v.iter().advance |i| {
info!("%?", *i);
}

View file

@ -8,12 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::vec;
fn want_slice(v: &[int]) -> int {
let mut sum = 0;
for vec::each(v) |i| { sum += *i; }
return sum;
for v.iter().advance |i| { sum += *i; }
sum
}
fn has_mut_vec(v: ~[int]) -> int {

View file

@ -16,7 +16,8 @@ pub fn main() {
assert_eq!(i, 10);
loop { i += 1; if i == 20 { break; } }
assert_eq!(i, 20);
for vec::each(~[1, 2, 3, 4, 5, 6]) |x| {
let xs = [1, 2, 3, 4, 5, 6];
for xs.iter().advance |x| {
if *x == 3 { break; } assert!((*x <= 3));
}
i = 0;
@ -26,7 +27,8 @@ pub fn main() {
i += 1; if i % 2 == 0 { loop; } assert!((i % 2 != 0));
if i >= 10 { break; }
}
for vec::each(~[1, 2, 3, 4, 5, 6]) |x| {
let ys = ~[1, 2, 3, 4, 5, 6];
for ys.iter().advance |x| {
if *x % 2 == 0 { loop; }
assert!((*x % 2 != 0));
}

View file

@ -61,29 +61,8 @@ impl<T> Mutable for cat<T> {
}
impl<T> Map<int, T> for cat<T> {
fn each<'a>(&'a self, f: &fn(&int, &'a T) -> bool) -> bool {
let mut n = int::abs(self.meows);
while n > 0 {
if !f(&n, &self.name) { return false; }
n -= 1;
}
return true;
}
fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
fn each_key(&self, f: &fn(v: &int) -> bool) -> bool {
self.each(|k, _| f(k))
}
fn each_value<'a>(&'a self, f: &fn(v: &'a T) -> bool) -> bool {
self.each(|_, v| f(v))
}
fn mutate_values(&mut self, _f: &fn(&int, &mut T) -> bool) -> bool {
fail!("nope")
}
fn insert(&mut self, k: int, _: T) -> bool {
self.meows += k;
true

View file

@ -8,7 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
// xfail-test
// FIXME: #7385: hits a codegen bug on OS X x86_64
/*!
* Try to double-check that static fns have the right size (with or
@ -23,6 +24,6 @@ struct S<'self>(&'self fn());
static closures: &'static [S<'static>] = &[S(f), S(f)];
pub fn main() {
for std::vec::each(bare_fns) |&bare_fn| { bare_fn() }
for std::vec::each(closures) |&closure| { (*closure)() }
for bare_fns.iter().advance |&bare_fn| { bare_fn() }
for closures.iter().advance |&closure| { (*closure)() }
}

View file

@ -8,10 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-test: #3511: does not currently compile, due to rvalue issues
use std::vec;
struct Pair { x: int, y: int }
pub fn main() {
for vec::each(~[Pair {x: 10, y: 20}, Pair {x: 30, y: 0}]) |elt| {
assert_eq!(elt.x + elt.y, 30);

View file

@ -18,7 +18,7 @@ trait sum {
impl<'self> sum for &'self [int] {
fn sum(self) -> int {
let mut sum = 0;
for vec::each(self) |e| { sum += *e; }
for self.iter().advance |e| { sum += *e; }
return sum;
}
}

View file

@ -31,7 +31,10 @@ trait map<T> {
impl<T> map<T> for ~[T] {
fn map<U:Copy>(&self, f: &fn(&T) -> U) -> ~[U] {
let mut r = ~[];
for std::vec::each(*self) |x| { r += ~[f(x)]; }
// FIXME: #7355 generates bad code with Iterator
for std::uint::range(0, self.len()) |i| {
r += ~[f(&self[i])];
}
r
}
}