about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2016-10-25 15:50:52 +0200
committerUlrik Sverdrup <bluss@users.noreply.github.com>2016-10-25 15:50:52 +0200
commit780acda325772b15f12f08f60ca2d4ba558cee51 (patch)
tree59b963d1896071e8790ee91de434fa731103d6ee
parent15a95866b4dd7a64875a8388aee296d319fc497d (diff)
downloadrust-780acda325772b15f12f08f60ca2d4ba558cee51.tar.gz
rust-780acda325772b15f12f08f60ca2d4ba558cee51.zip
iter: Implement .fold() for .cloned() and .map()
Implement .fold() specifically for .map() and .cloned() so that any
inner fold improvements are available through map and cloned.
-rw-r--r--src/libcore/iter/mod.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs
index 9eeb2608071..2c3b8864a11 100644
--- a/src/libcore/iter/mod.rs
+++ b/src/libcore/iter/mod.rs
@@ -399,6 +399,12 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
     fn size_hint(&self) -> (usize, Option<usize>) {
         self.it.size_hint()
     }
+
+    fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
+        where F: FnMut(Acc, Self::Item) -> Acc,
+    {
+        self.it.fold(init, move |acc, elt| f(acc, elt.clone()))
+    }
 }
 
 #[stable(feature = "iter_cloned", since = "1.1.0")]
@@ -939,6 +945,13 @@ impl<B, I: Iterator, F> Iterator for Map<I, F> where F: FnMut(I::Item) -> B {
     fn size_hint(&self) -> (usize, Option<usize>) {
         self.iter.size_hint()
     }
+
+    fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
+        where G: FnMut(Acc, Self::Item) -> Acc,
+    {
+        let mut f = self.f;
+        self.iter.fold(init, move |acc, elt| g(acc, f(elt)))
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]