about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2012-09-28 16:37:14 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-09-28 17:57:02 -0700
commita3a257cc3b29cb134b05a72adbfeff08f1e7a98c (patch)
tree0057d14c69772c162546eeb23fde82957c8f36ec /src
parentfdc60621364eef2fc20974ff4411f31099ed9595 (diff)
Demode iter::foldl and friends
Diffstat (limited to 'src')
-rw-r--r--src/libcore/dlist.rs2
-rw-r--r--src/libcore/iter-trait.rs12
-rw-r--r--src/libcore/iter.rs41
-rw-r--r--src/libcore/vec.rs12
-rw-r--r--src/rustc/middle/liveness.rs2
-rw-r--r--src/rustc/middle/trans/meth.rs4
-rw-r--r--src/rustc/middle/ty.rs10
-rw-r--r--src/rustc/middle/typeck/check/regionmanip.rs4
-rw-r--r--src/test/run-pass/iter-foldl.rs2
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<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) }
 }
 
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<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,
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> &[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) }
 }
 // ___________________________________________________________________________
 
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;