2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-11-04 20:41:00 -08:00
|
|
|
// xfail-test FIXME #2263
|
2012-09-19 13:59:44 -07:00
|
|
|
// xfail-fast
|
2012-07-31 10:27:51 -07:00
|
|
|
// This test had to do with an outdated version of the iterable trait.
|
2012-04-11 21:45:18 -07: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 15:52:21 -07:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait iterable<A> {
|
2012-04-11 21:45:18 -07:00
|
|
|
fn iter(blk: fn(A));
|
|
|
|
}
|
|
|
|
|
2013-02-14 11:47:00 -08:00
|
|
|
impl<A> iterable<A> for fn@(fn(A)) {
|
2012-04-11 21:45:18 -07:00
|
|
|
fn iter(blk: fn(A)) { self(blk); }
|
|
|
|
}
|
|
|
|
|
2013-02-14 11:47:00 -08:00
|
|
|
impl iterable<uint> for fn@(fn(uint)) {
|
2012-09-23 04:39:27 -07:00
|
|
|
fn iter(blk: fn(&&v: uint)) { self( |i| blk(i) ) }
|
2012-04-11 21:45:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn filter<A,IA:iterable<A>>(self: IA, prd: fn@(A) -> bool, blk: fn(A)) {
|
2012-06-30 16:19:07 -07:00
|
|
|
do self.iter |a| {
|
2012-04-11 21:45:18 -07:00
|
|
|
if prd(a) { blk(a) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foldl<A,B,IA:iterable<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
2013-02-15 02:44:18 -08:00
|
|
|
let mut b = b0;
|
2012-06-30 16:19:07 -07:00
|
|
|
do self.iter |a| {
|
2013-02-15 02:44:18 -08:00
|
|
|
b = blk(b, a);
|
2012-04-11 21:45:18 -07:00
|
|
|
}
|
2013-02-15 02:44:18 -08:00
|
|
|
b
|
2012-04-11 21:45:18 -07:00
|
|
|
}
|
2012-04-23 09:25:14 +02:00
|
|
|
|
2012-05-26 00:32:08 -07:00
|
|
|
fn range(lo: uint, hi: uint, it: fn(uint)) {
|
|
|
|
let mut i = lo;
|
|
|
|
while i < hi {
|
|
|
|
it(i);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2012-08-10 18:15:08 -07:00
|
|
|
let range: fn@(fn&(uint)) = |a| range(0u, 1000u, a);
|
2012-09-23 04:39:27 -07:00
|
|
|
let filt: fn@(fn&(&&v: uint)) = |a| filter(
|
2012-04-11 21:45:18 -07:00
|
|
|
range,
|
2012-06-30 16:19:07 -07:00
|
|
|
|&&n: uint| n % 3u != 0u && n % 5u != 0u,
|
|
|
|
a);
|
|
|
|
let sum = foldl(filt, 0u, |accum, &&n: uint| accum + n );
|
2012-04-23 09:25:14 +02:00
|
|
|
|
2012-08-22 17:24:52 -07:00
|
|
|
io::println(fmt!("%u", sum));
|
2012-08-10 18:15:08 -07:00
|
|
|
}
|