rust/src/libcore/iter-trait.rs

53 lines
1.7 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.
2012-08-27 18:26:35 -05:00
import cmp::{Eq, Ord};
import inst::{IMPL_T, EACH, SIZE_HINT};
export extensions;
2012-08-14 18:54:13 -05:00
impl<A> IMPL_T<A>: iter::BaseIter<A> {
pure fn each(blk: fn(A) -> bool) { EACH(self, blk) }
2012-08-20 14:23:37 -05:00
pure fn size_hint() -> Option<uint> { SIZE_HINT(self) }
}
2012-08-14 18:54:13 -05:00
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
iter::foldl(self, b0, blk)
}
2012-08-27 18:26:35 -05:00
}
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
pure fn contains(x: A) -> bool { iter::contains(self, x) }
pure fn count(x: A) -> uint { iter::count(self, x) }
2012-08-20 14:23:37 -05:00
pure fn position(f: fn(A) -> bool) -> Option<uint> {
2012-06-25 17:04:00 -05:00
iter::position(self, f)
}
}
2012-08-14 18:54:13 -05:00
impl<A: copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure 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)
// }
2012-08-27 18:26:35 -05:00
pure fn find(p: fn(A) -> bool) -> Option<A> { iter::find(self, p) }
}
impl<A: copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
pure fn min() -> A { iter::min(self) }
pure fn max() -> A { iter::max(self) }
}
2012-08-27 18:26:35 -05:00