Demode iter::foldl and friends

This commit is contained in:
Tim Chevalier 2012-09-28 16:37:14 -07:00
parent fdc6062136
commit a3a257cc3b
9 changed files with 42 additions and 47 deletions

View File

@ -675,7 +675,7 @@ mod tests {
#[test]
fn test_dlist_foldl() {
let l = from_vec(vec::from_fn(101, |x|x));
assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050;
assert iter::foldl(&l, 0, |accum,elem| *accum+*elem) == 5050;
}
#[test]
fn test_dlist_break_early() {

View File

@ -16,8 +16,8 @@ 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 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> {
iter::position(self, f)
@ -26,7 +26,7 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
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 count(x: &A) -> uint { iter::count(&self, x) }
}
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
@ -36,7 +36,7 @@ impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op)
}
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
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] {
@ -47,7 +47,7 @@ impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
}
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() -> A { iter::min(&self) }
pure fn max() -> A { iter::max(&self) }
}

View File

@ -18,7 +18,7 @@ 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 foldl<B>(+b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(f: fn(A) -> bool) -> Option<uint>;
}
@ -118,16 +118,17 @@ pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
}
}
pure fn foldl<A,B,IA:BaseIter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
-> B {
let mut b <- b0;
for self.each |a| {
b = blk(b, *a);
b = blk(&b, a);
}
move b
}
pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: IA) -> ~[A] {
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy r, ~[a]))
pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(*r, ~[*a]))
}
pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
@ -137,12 +138,12 @@ pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
return false;
}
pure fn count<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> uint {
pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
do foldl(self, 0) |count, value| {
if value == *x {
count + 1
if *value == *x {
*count + 1
} else {
count
*count
}
}
}
@ -170,16 +171,13 @@ pure fn repeat(times: uint, blk: fn() -> bool) {
}
}
// XXX bad copies
pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
match a {
Some(copy a_) if a_ < b => {
// FIXME (#2005): Not sure if this is successfully optimized to
// a move
a
&Some(a_) if a_ < *b => {
*(move a)
}
_ => Some(b)
_ => Some(*b)
}
} {
Some(move val) => val,
@ -187,16 +185,13 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
}
}
// XXX bad copies
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
match a {
Some(copy a_) if a_ > b => {
// FIXME (#2005): Not sure if this is successfully optimized to
// a move.
a
&Some(a_) if a_ > *b => {
*(move a)
}
_ => Some(b)
_ => Some(*b)
}
} {
Some(move val) => val,

View File

@ -1993,8 +1993,8 @@ impl<A> &[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 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> {
iter::position(self, f)
@ -2003,7 +2003,7 @@ impl<A> &[A]: iter::ExtendedIter<A> {
impl<A: Eq> &[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 count(x: &A) -> uint { iter::count(&self, x) }
}
impl<A: Copy> &[A]: iter::CopyableIter<A> {
@ -2013,7 +2013,7 @@ impl<A: Copy> &[A]: iter::CopyableIter<A> {
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op)
}
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }
// 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] {
@ -2024,8 +2024,8 @@ impl<A: Copy> &[A]: iter::CopyableIter<A> {
}
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
pure fn min() -> A { iter::min(self) }
pure fn max() -> A { iter::max(self) }
pure fn min() -> A { iter::min(&self) }
pure fn max() -> A { iter::max(&self) }
}
// ___________________________________________________________________________

View File

@ -1014,7 +1014,7 @@ impl Liveness {
fn propagate_through_opt_expr(opt_expr: Option<@expr>,
succ: LiveNode) -> LiveNode {
do opt_expr.foldl(succ) |succ, expr| {
self.propagate_through_expr(expr, succ)
self.propagate_through_expr(*expr, *succ)
}
}

View File

@ -364,8 +364,8 @@ fn combine_impl_and_methods_origins(bcx: block,
// Flatten out to find the number of vtables the method expects.
let m_vtables = m_boundss.foldl(0, |sum, m_bounds| {
m_bounds.foldl(sum, |sum, m_bound| {
sum + match m_bound {
m_bounds.foldl(*sum, |sum, m_bound| {
(*sum) + match (*m_bound) {
ty::bound_copy | ty::bound_owned |
ty::bound_send | ty::bound_const => 0,
ty::bound_trait(_) => 1

View File

@ -2137,25 +2137,25 @@ fn type_size(cx: ctxt, ty: t) -> uint {
}
ty_rec(flds) => {
flds.foldl(0, |s, f| s + type_size(cx, f.mt.ty))
flds.foldl(0, |s, f| *s + type_size(cx, f.mt.ty))
}
ty_class(did, ref substs) => {
let flds = class_items_as_fields(cx, did, substs);
flds.foldl(0, |s, f| s + type_size(cx, f.mt.ty))
flds.foldl(0, |s, f| *s + type_size(cx, f.mt.ty))
}
ty_tup(tys) => {
tys.foldl(0, |s, t| s + type_size(cx, t))
tys.foldl(0, |s, t| *s + type_size(cx, *t))
}
ty_enum(did, ref substs) => {
let variants = substd_enum_variants(cx, did, substs);
variants.foldl( // find max size of any variant
0,
|m, v| uint::max(m,
|m, v| uint::max(*m,
// find size of this variant:
v.args.foldl(0, |s, a| s + type_size(cx, a))))
v.args.foldl(0, |s, a| *s + type_size(cx, *a))))
}
ty_param(_) | ty_self => {

View File

@ -111,14 +111,14 @@ fn replace_bound_regions_in_fn_ty(
// For each type `ty` in `tys`...
do tys.foldl(isr) |isr, ty| {
let mut isr = isr;
let mut isr = *isr;
// Using fold_regions is inefficient, because it
// constructs new types, but it avoids code duplication in
// terms of locating all the regions within the various
// kinds of types. This had already caused me several
// bugs so I decided to switch over.
do ty::fold_regions(tcx, ty) |r, in_fn| {
do ty::fold_regions(tcx, *ty) |r, in_fn| {
if !in_fn { isr = append_isr(isr, to_r, r); }
r
};

View File

@ -1,4 +1,4 @@
fn add(&&x: float, &&y: uint) -> float { x + (y as float) }
fn add(x: &float, y: &uint) -> float { *x + ((*y) as float) }
fn main() {
assert [1u, 3u]/_.foldl(20f, add) == 24f;