Skip to content

Update iter with &self and passing around references #4214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libcore/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ impl T: iter::Times {
will execute the given function exactly x times. If we assume that \
`x` is an int, this is functionally equivalent to \
`for int::range(0, x) |_i| { /* anything */ }`."]
pure fn times(it: fn() -> bool) {
if self < 0 {
pure fn times(&self, it: fn() -> bool) {
if *self < 0 {
fail fmt!("The .times method expects a nonnegative number, \
but found %?", self);
}
let mut i = self;
let mut i = *self;
while i > 0 {
if !it() { break }
i -= 1;
Expand Down
57 changes: 32 additions & 25 deletions src/libcore/iter-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,53 @@ use cmp::{Eq, Ord};
use self::inst::{IMPL_T, EACH, SIZE_HINT};

impl<A> IMPL_T<A>: iter::BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool) { EACH(&self, blk) }
pure fn size_hint() -> Option<uint> { SIZE_HINT(&self) }
pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) }
pure fn size_hint(&self) -> Option<uint> { SIZE_HINT(self) }
}

impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &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, move b0, blk)
pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
iter::eachi(self, blk)
}
pure fn position(f: fn(&A) -> bool) -> Option<uint> {
iter::position(&self, f)
pure fn all(&self, blk: fn(&A) -> bool) -> bool {
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)
}
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}

}

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) }
pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
}

impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A] {
iter::filter_to_vec(&self, pred)
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op)
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
pure fn find(&self, f: fn(&A) -> bool) -> Option<A> {
iter::find(self, f)
}
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }

pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(a: A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(&self, op)
}

pure fn find(p: fn(a: 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) }
pure fn min(&self) -> A { iter::min(self) }
pure fn max(&self) -> A { iter::max(self) }
}

65 changes: 33 additions & 32 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,39 @@ use cmp::{Eq, Ord};
pub type InitOp<T> = &fn(uint) -> T;

pub trait BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool);
pure fn size_hint() -> Option<uint>;
pure fn each(&self, blk: fn(v: &A) -> bool);
pure fn size_hint(&self) -> Option<uint>;
}

pub trait ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool);
pure fn all(blk: fn(&A) -> bool) -> bool;
pure fn any(blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(f: fn(&A) -> bool) -> Option<uint>;
pure fn eachi(&self, blk: fn(uint, v: &A) -> bool);
pure fn all(&self, blk: fn(&A) -> bool) -> bool;
pure fn any(&self, blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint>;
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B];
pure fn flat_map_to_vec<B,IB: BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B];
}

pub trait EqIter<A:Eq> {
pure fn contains(x: &A) -> bool;
pure fn count(x: &A) -> uint;
pure fn contains(&self, x: &A) -> bool;
pure fn count(&self, x: &A) -> uint;
}

pub trait Times {
pure fn times(it: fn() -> bool);
pure fn times(&self, it: fn() -> bool);
}

pub trait CopyableIter<A:Copy> {
pure fn filter_to_vec(pred: fn(a: A) -> bool) -> ~[A];
pure fn map_to_vec<B>(op: fn(v: A) -> B) -> ~[B];
pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(op: fn(A) -> IB) -> ~[B];
pure fn to_vec() -> ~[A];
pure fn find(p: fn(a: A) -> bool) -> Option<A>;
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A];
pure fn to_vec(&self) -> ~[A];
pure fn find(&self, p: fn(&A) -> bool) -> Option<A>;
}

pub trait CopyableOrderedIter<A:Copy Ord> {
pure fn min() -> A;
pure fn max() -> A;
pure fn min(&self) -> A;
pure fn max(&self) -> A;
}

pub trait CopyableNonstrictIter<A:Copy> {
Expand All @@ -81,11 +82,11 @@ pub trait Buildable<A> {
* onto the sequence being constructed.
*/
static pure fn build_sized(size: uint,
builder: fn(push: pure fn(v: A))) -> self;
builder: fn(push: pure fn(A))) -> self;
}

pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
blk: fn(uint, v: &A) -> bool) {
blk: fn(uint, &A) -> bool) {
let mut i = 0;
for self.each |a| {
if !blk(i, a) { break; }
Expand All @@ -110,30 +111,30 @@ pub pure fn any<A,IA:BaseIter<A>>(self: &IA,
}

pub pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(
self: &IA, prd: fn(a: A) -> bool) -> ~[A] {
self: &IA, prd: fn(&A) -> bool) -> ~[A] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
if prd(*a) { push(*a); }
if prd(a) { push(*a); }
}
}
}

pub pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA,
op: fn(v: A) -> B)
pub pure fn map_to_vec<A,B,IA:BaseIter<A>>(self: &IA,
op: fn(&A) -> B)
-> ~[B] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
push(op(*a));
push(op(a));
}
}
}

pub pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
self: &IA, op: fn(a: A) -> IB) -> ~[B] {
pub pure fn flat_map_to_vec<A,B,IA:BaseIter<A>,IB:BaseIter<B>>(
self: &IA, op: fn(&A) -> IB) -> ~[B] {
do vec::build |push| {
for self.each |a| {
for op(*a).each |b| {
push(*b);
for op(a).each |&b| {
push(b);
}
}
}
Expand Down Expand Up @@ -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,
p: fn(a: A) -> bool) -> Option<A> {
f: fn(&A) -> bool) -> Option<A> {
for self.each |i| {
if p(*i) { return Some(*i) }
if f(i) { return Some(*i) }
}
return None;
}
Expand All @@ -242,7 +243,7 @@ pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
* onto the sequence being constructed.
*/
#[inline(always)]
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(v: A)))
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(A)))
-> B {
Buildable::build_sized(4, builder)
}
Expand All @@ -263,7 +264,7 @@ pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(v: A)))
#[inline(always)]
pub pure fn build_sized_opt<A,B: Buildable<A>>(
size: Option<uint>,
builder: fn(push: pure fn(v: A))) -> B {
builder: fn(push: pure fn(A))) -> B {

Buildable::build_sized(size.get_default(4), builder)
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/uint-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl T: iter::Times {
will execute the given function exactly x times. If we assume that \
`x` is an int, this is functionally equivalent to \
`for int::range(0, x) |_i| { /* anything */ }`."]
pure fn times(it: fn() -> bool) {
let mut i = self;
pure fn times(&self, it: fn() -> bool) {
let mut i = *self;
while i > 0 {
if !it() { break }
i -= 1;
Expand Down
Loading