From a3a257cc3b29cb134b05a72adbfeff08f1e7a98c Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 28 Sep 2012 16:37:14 -0700 Subject: [PATCH] Demode iter::foldl and friends --- src/libcore/dlist.rs | 2 +- src/libcore/iter-trait.rs | 12 +++--- src/libcore/iter.rs | 41 +++++++++----------- src/libcore/vec.rs | 12 +++--- src/rustc/middle/liveness.rs | 2 +- src/rustc/middle/trans/meth.rs | 4 +- src/rustc/middle/ty.rs | 10 ++--- src/rustc/middle/typeck/check/regionmanip.rs | 4 +- src/test/run-pass/iter-foldl.rs | 2 +- 9 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/libcore/dlist.rs b/src/libcore/dlist.rs index 0bdf5caec0c..4e08dd4c2f3 100644 --- a/src/libcore/dlist.rs +++ b/src/libcore/dlist.rs @@ -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() { diff --git a/src/libcore/iter-trait.rs b/src/libcore/iter-trait.rs index 98fd0e27e97..a6acd1c040e 100644 --- a/src/libcore/iter-trait.rs +++ b/src/libcore/iter-trait.rs @@ -16,8 +16,8 @@ impl IMPL_T: iter::ExtendedIter { 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(+b0: B, blk: fn(B, A) -> B) -> B { - iter::foldl(self, move b0, blk) + pure fn foldl(+b0: B, blk: fn(&B, &A) -> B) -> B { + iter::foldl(&self, move b0, blk) } pure fn position(f: fn(A) -> bool) -> Option { iter::position(self, f) @@ -26,7 +26,7 @@ impl IMPL_T: iter::ExtendedIter { impl IMPL_T: iter::EqIter { 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 IMPL_T: iter::CopyableIter { @@ -36,7 +36,7 @@ impl IMPL_T: iter::CopyableIter { pure fn map_to_vec(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>(op: fn(+a: A) -> IB) -> ~[B] { @@ -47,7 +47,7 @@ impl IMPL_T: iter::CopyableIter { } impl IMPL_T: iter::CopyableOrderedIter { - 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) } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 2db68107fc3..77f9abe8e0b 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -18,7 +18,7 @@ trait ExtendedIter { 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(+b0: B, blk: fn(B, A) -> B) -> B; + pure fn foldl(+b0: B, blk: fn(&B, &A) -> B) -> B; pure fn position(f: fn(A) -> bool) -> Option; } @@ -118,16 +118,17 @@ pure fn flat_map_to_vec,IB:BaseIter>( } } -pure fn foldl>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B { +pure fn foldl>(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>(self: IA) -> ~[A] { - foldl::(self, ~[], |r, a| vec::append(copy r, ~[a])) +pure fn to_vec>(self: &IA) -> ~[A] { + foldl::(self, ~[], |r, a| vec::append(*r, ~[*a])) } pure fn contains>(self: IA, x: &A) -> bool { @@ -137,12 +138,12 @@ pure fn contains>(self: IA, x: &A) -> bool { return false; } -pure fn count>(self: IA, x: &A) -> uint { +pure fn count>(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>(self: IA) -> A { +pure fn min>(self: &IA) -> A { match do foldl::,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>(self: IA) -> A { } } -// XXX bad copies -pure fn max>(self: IA) -> A { +pure fn max>(self: &IA) -> A { match do foldl::,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, diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 2044bbe42c9..66b1c0d95ec 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1993,8 +1993,8 @@ impl &[A]: iter::ExtendedIter { 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(+b0: B, blk: fn(B, A) -> B) -> B { - iter::foldl(self, move b0, blk) + pure fn foldl(+b0: B, blk: fn(&B, &A) -> B) -> B { + iter::foldl(&self, move b0, blk) } pure fn position(f: fn(A) -> bool) -> Option { iter::position(self, f) @@ -2003,7 +2003,7 @@ impl &[A]: iter::ExtendedIter { impl &[A]: iter::EqIter { 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]: iter::CopyableIter { @@ -2013,7 +2013,7 @@ impl &[A]: iter::CopyableIter { pure fn map_to_vec(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>(op: fn(A) -> IB) -> ~[B] { @@ -2024,8 +2024,8 @@ impl &[A]: iter::CopyableIter { } impl &[A]: iter::CopyableOrderedIter { - 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) } } // ___________________________________________________________________________ diff --git a/src/rustc/middle/liveness.rs b/src/rustc/middle/liveness.rs index 90c1b6ba54e..90762c5b714 100644 --- a/src/rustc/middle/liveness.rs +++ b/src/rustc/middle/liveness.rs @@ -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) } } diff --git a/src/rustc/middle/trans/meth.rs b/src/rustc/middle/trans/meth.rs index 33df35cca52..ebc1645e3d8 100644 --- a/src/rustc/middle/trans/meth.rs +++ b/src/rustc/middle/trans/meth.rs @@ -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 diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs index 397a1cd6aa1..973db90ff66 100644 --- a/src/rustc/middle/ty.rs +++ b/src/rustc/middle/ty.rs @@ -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 => { diff --git a/src/rustc/middle/typeck/check/regionmanip.rs b/src/rustc/middle/typeck/check/regionmanip.rs index 29d4e9927ff..4afb3ad78a6 100644 --- a/src/rustc/middle/typeck/check/regionmanip.rs +++ b/src/rustc/middle/typeck/check/regionmanip.rs @@ -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 }; diff --git a/src/test/run-pass/iter-foldl.rs b/src/test/run-pass/iter-foldl.rs index 41bb6b82ddd..bbc1673f686 100644 --- a/src/test/run-pass/iter-foldl.rs +++ b/src/test/run-pass/iter-foldl.rs @@ -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;