rust/src/libcore/iter-trait.rs

49 lines
1.6 KiB
Rust
Raw Normal View History

// This makes use of a clever hack that brson came up with to
// workaround our lack of traits and lack of macros. See core.{rc,rs} for
// how this file is used.
import inst::{IMPL_T, EACH, SIZE_HINT};
export extensions;
2012-08-07 20:10:06 -05:00
impl<A> IMPL_T<A>: iter::base_iter<A> {
fn each(blk: fn(A) -> bool) { EACH(self, blk) }
fn size_hint() -> option<uint> { SIZE_HINT(self) }
}
2012-08-07 20:10:06 -05:00
impl<A> IMPL_T<A>: iter::extended_iter<A> {
fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
iter::foldl(self, b0, blk)
}
fn contains(x: A) -> bool { iter::contains(self, x) }
fn count(x: A) -> uint { iter::count(self, x) }
2012-06-25 17:04:00 -05:00
fn position(f: fn(A) -> bool) -> option<uint> {
iter::position(self, f)
}
}
2012-08-07 20:10:06 -05:00
impl<A: copy> IMPL_T<A>: iter::copyable_iter<A> {
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] { iter::map_to_vec(self, op) }
fn to_vec() -> ~[A] { iter::to_vec(self) }
2012-06-14 19:57:22 -05:00
// FIXME--bug in resolve prevents this from working (#2611)
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
// iter::flat_map_to_vec(self, op)
// }
fn min() -> A { iter::min(self) }
fn max() -> A { iter::max(self) }
fn find(p: fn(A) -> bool) -> option<A> {
for self.each |i| {
2012-08-01 19:30:05 -05:00
if p(i) { return some(i) }
}
2012-08-01 19:30:05 -05:00
return none;
}
}