2012-09-19 15:59:44 -05:00
|
|
|
// xfail-fast
|
2012-07-31 12:27:51 -05:00
|
|
|
// This test had to do with an outdated version of the iterable trait.
|
2012-04-11 23:45:18 -05:00
|
|
|
// However, the condition it was testing seemed complex enough to
|
|
|
|
// warrant still having a test, so I inlined the old definitions.
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-07-31 12:27:51 -05:00
|
|
|
trait iterable<A> {
|
2012-04-11 23:45:18 -05:00
|
|
|
fn iter(blk: fn(A));
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<A> fn@(fn(A)): iterable<A> {
|
2012-04-11 23:45:18 -05:00
|
|
|
fn iter(blk: fn(A)) { self(blk); }
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl fn@(fn(uint)): iterable<uint> {
|
2012-09-23 06:39:27 -05:00
|
|
|
fn iter(blk: fn(&&v: uint)) { self( |i| blk(i) ) }
|
2012-04-11 23:45:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn filter<A,IA:iterable<A>>(self: IA, prd: fn@(A) -> bool, blk: fn(A)) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do self.iter |a| {
|
2012-04-11 23:45:18 -05:00
|
|
|
if prd(a) { blk(a) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foldl<A,B,IA:iterable<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
|
|
|
let mut b <- b0;
|
2012-06-30 18:19:07 -05:00
|
|
|
do self.iter |a| {
|
2012-04-11 23:45:18 -05:00
|
|
|
b <- blk(b, a);
|
|
|
|
}
|
2012-09-19 00:45:24 -05:00
|
|
|
move b
|
2012-04-11 23:45:18 -05:00
|
|
|
}
|
2012-04-23 02:25:14 -05:00
|
|
|
|
2012-05-26 02:32:08 -05:00
|
|
|
fn range(lo: uint, hi: uint, it: fn(uint)) {
|
|
|
|
let mut i = lo;
|
|
|
|
while i < hi {
|
|
|
|
it(i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 02:25:14 -05:00
|
|
|
fn main() {
|
2012-08-10 20:15:08 -05:00
|
|
|
let range: fn@(fn&(uint)) = |a| range(0u, 1000u, a);
|
2012-09-23 06:39:27 -05:00
|
|
|
let filt: fn@(fn&(&&v: uint)) = |a| filter(
|
2012-04-11 23:45:18 -05:00
|
|
|
range,
|
2012-06-30 18:19:07 -05:00
|
|
|
|&&n: uint| n % 3u != 0u && n % 5u != 0u,
|
|
|
|
a);
|
|
|
|
let sum = foldl(filt, 0u, |accum, &&n: uint| accum + n );
|
2012-04-23 02:25:14 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::println(fmt!("%u", sum));
|
2012-08-10 20:15:08 -05:00
|
|
|
}
|