core: Add &self to core::iter methods

This commit is contained in:
Erick Tryzelaar
2012-12-11 09:06:56 -08:00
parent 3fc74df7a4
commit 88962eeed8
5 changed files with 83 additions and 70 deletions

View File

@@ -98,12 +98,12 @@ impl T: iter::Times {
will execute the given function exactly x times. If we assume that \ will execute the given function exactly x times. If we assume that \
`x` is an int, this is functionally equivalent to \ `x` is an int, this is functionally equivalent to \
`for int::range(0, x) |_i| { /* anything */ }`."] `for int::range(0, x) |_i| { /* anything */ }`."]
pure fn times(it: fn() -> bool) { pure fn times(&self, it: fn() -> bool) {
if self < 0 { if *self < 0 {
fail fmt!("The .times method expects a nonnegative number, \ fail fmt!("The .times method expects a nonnegative number, \
but found %?", self); but found %?", self);
} }
let mut i = self; let mut i = *self;
while i > 0 { while i > 0 {
if !it() { break } if !it() { break }
i -= 1; i -= 1;

View File

@@ -20,46 +20,53 @@ use cmp::{Eq, Ord};
use self::inst::{IMPL_T, EACH, SIZE_HINT}; use self::inst::{IMPL_T, EACH, SIZE_HINT};
impl<A> IMPL_T<A>: iter::BaseIter<A> { impl<A> IMPL_T<A>: iter::BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool) { EACH(&self, blk) } pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) }
pure fn size_hint() -> Option<uint> { SIZE_HINT(&self) } pure fn size_hint(&self) -> Option<uint> { SIZE_HINT(self) }
} }
impl<A> IMPL_T<A>: iter::ExtendedIter<A> { impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) } pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) } iter::eachi(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, move b0, blk)
} }
pure fn position(f: fn(&A) -> bool) -> Option<uint> { pure fn all(&self, blk: fn(&A) -> bool) -> bool {
iter::position(&self, f) iter::all(self, blk)
}
pure fn any(&self, blk: fn(&A) -> bool) -> bool {
iter::any(self, blk)
}
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
iter::foldl(self, move b0, blk)
}
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
} }
} }
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> { impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
pure fn contains(x: &A) -> bool { iter::contains(&self, x) } pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
pure fn count(x: &A) -> uint { iter::count(&self, x) } pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
} }
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> { impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A] { pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
iter::filter_to_vec(&self, pred) iter::filter_to_vec(self, pred)
} }
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B] { pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op) iter::map_to_vec(self, op)
} }
pure fn to_vec() -> ~[A] { iter::to_vec(&self) } pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(a: A) -> IB) pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(a: A) -> IB)
-> ~[B] { -> ~[B] {
iter::flat_map_to_vec(&self, op) iter::flat_map_to_vec(self, op)
}
pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
iter::find(self, f)
} }
pure fn find(p: fn(a: A) -> bool) -> Option<A> { iter::find(&self, p) }
} }
impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> { impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
pure fn min() -> A { iter::min(&self) } pure fn min(&self) -> A { iter::min(self) }
pure fn max() -> A { iter::max(&self) } pure fn max(&self) -> A { iter::max(self) }
} }

View File

@@ -23,38 +23,39 @@ use cmp::{Eq, Ord};
pub type InitOp<T> = &fn(uint) -> T; pub type InitOp<T> = &fn(uint) -> T;
pub trait BaseIter<A> { pub trait BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool); pure fn each(&self, blk: fn(v: &A) -> bool);
pure fn size_hint() -> Option<uint>; pure fn size_hint(&self) -> Option<uint>;
} }
pub trait ExtendedIter<A> { pub trait ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool); pure fn eachi(&self, blk: fn(uint, v: &A) -> bool);
pure fn all(blk: fn(&A) -> bool) -> bool; pure fn all(&self, blk: fn(&A) -> bool) -> bool;
pure fn any(blk: fn(&A) -> bool) -> bool; pure fn any(&self, blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B; pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(f: fn(&A) -> bool) -> Option<uint>; pure fn position(&self, f: fn(&A) -> bool) -> Option<uint>;
} }
pub trait EqIter<A:Eq> { pub trait EqIter<A:Eq> {
pure fn contains(x: &A) -> bool; pure fn contains(&self, x: &A) -> bool;
pure fn count(x: &A) -> uint; pure fn count(&self, x: &A) -> uint;
} }
pub trait Times { pub trait Times {
pure fn times(it: fn() -> bool); pure fn times(&self, it: fn() -> bool);
} }
pub trait CopyableIter<A:Copy> { pub trait CopyableIter<A:Copy> {
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A]; pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A];
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B]; pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B];
pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(op: fn(A) -> IB) -> ~[B]; pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(&self, op: fn(A) -> IB)
pure fn to_vec() -> ~[A]; -> ~[B];
pure fn find(p: fn(a: A) -> bool) -> Option<A>; pure fn to_vec(&self) -> ~[A];
pure fn find(&self, p: fn(A) -> bool) -> Option<A>;
} }
pub trait CopyableOrderedIter<A:Copy Ord> { pub trait CopyableOrderedIter<A:Copy Ord> {
pure fn min() -> A; pure fn min(&self) -> A;
pure fn max() -> A; pure fn max(&self) -> A;
} }
pub trait CopyableNonstrictIter<A:Copy> { pub trait CopyableNonstrictIter<A:Copy> {
@@ -222,9 +223,9 @@ pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
} }
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA, pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
p: fn(a: A) -> bool) -> Option<A> { f: fn(A) -> bool) -> Option<A> {
for self.each |i| { for self.each |i| {
if p(*i) { return Some(*i) } if f(*i) { return Some(*i) }
} }
return None; return None;
} }

View File

@@ -92,8 +92,8 @@ impl T: iter::Times {
will execute the given function exactly x times. If we assume that \ will execute the given function exactly x times. If we assume that \
`x` is an int, this is functionally equivalent to \ `x` is an int, this is functionally equivalent to \
`for int::range(0, x) |_i| { /* anything */ }`."] `for int::range(0, x) |_i| { /* anything */ }`."]
pure fn times(it: fn() -> bool) { pure fn times(&self, it: fn() -> bool) {
let mut i = self; let mut i = *self;
while i > 0 { while i > 0 {
if !it() { break } if !it() { break }
i -= 1; i -= 1;

View File

@@ -2013,57 +2013,62 @@ pub mod bytes {
// required in the slice. // required in the slice.
impl<A> &[A]: iter::BaseIter<A> { impl<A> &[A]: iter::BaseIter<A> {
pub pure fn each(blk: fn(v: &A) -> bool) { pub pure fn each(&self, blk: fn(v: &A) -> bool) {
// FIXME(#2263)---should be able to call each(self, blk) // FIXME(#2263)---should be able to call each(self, blk)
for each(self) |e| { for each(*self) |e| {
if (!blk(e)) { if (!blk(e)) {
return; return;
} }
} }
} }
pure fn size_hint() -> Option<uint> { Some(len(self)) } pure fn size_hint(&self) -> Option<uint> { Some(len(*self)) }
} }
impl<A> &[A]: iter::ExtendedIter<A> { impl<A> &[A]: iter::ExtendedIter<A> {
pub pure fn eachi(blk: fn(uint, v: &A) -> bool) { pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
iter::eachi(&self, blk) iter::eachi(self, blk)
} }
pub pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) } pub pure fn all(&self, blk: fn(&A) -> bool) -> bool {
pub pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&self, blk) } iter::all(self, blk)
pub pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B {
iter::foldl(&self, b0, blk)
} }
pub pure fn position(f: fn(&A) -> bool) -> Option<uint> { pub pure fn any(&self, blk: fn(&A) -> bool) -> bool {
iter::position(&self, f) iter::any(self, blk)
}
pub pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
iter::foldl(self, b0, blk)
}
pub pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
} }
} }
impl<A: Eq> &[A]: iter::EqIter<A> { impl<A: Eq> &[A]: iter::EqIter<A> {
pub pure fn contains(x: &A) -> bool { iter::contains(&self, x) } pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
pub pure fn count(x: &A) -> uint { iter::count(&self, x) } pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
} }
impl<A: Copy> &[A]: iter::CopyableIter<A> { impl<A: Copy> &[A]: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A] { pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
iter::filter_to_vec(&self, pred) iter::filter_to_vec(self, pred)
} }
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B] { pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op) iter::map_to_vec(self, op)
} }
pure fn to_vec() -> ~[A] { iter::to_vec(&self) } pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(A) -> IB) -> ~[B] { pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(A) -> IB)
iter::flat_map_to_vec(&self, op) -> ~[B] {
iter::flat_map_to_vec(self, op)
} }
pub pure fn find(p: fn(a: A) -> bool) -> Option<A> { pub pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
iter::find(&self, p) iter::find(self, f)
} }
} }
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> { impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
pure fn min() -> A { iter::min(&self) } pure fn min(&self) -> A { iter::min(self) }
pure fn max() -> A { iter::max(&self) } pure fn max(&self) -> A { iter::max(self) }
} }
impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> { impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> {