Skip to content

Commit 780acda

Browse files
committed
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.
1 parent 15a9586 commit 780acda

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/libcore/iter/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ impl<'a, I, T: 'a> Iterator for Cloned<I>
399399
fn size_hint(&self) -> (usize, Option<usize>) {
400400
self.it.size_hint()
401401
}
402+
403+
fn fold<Acc, F>(self, init: Acc, mut f: F) -> Acc
404+
where F: FnMut(Acc, Self::Item) -> Acc,
405+
{
406+
self.it.fold(init, move |acc, elt| f(acc, elt.clone()))
407+
}
402408
}
403409

404410
#[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 {
939945
fn size_hint(&self) -> (usize, Option<usize>) {
940946
self.iter.size_hint()
941947
}
948+
949+
fn fold<Acc, G>(self, init: Acc, mut g: G) -> Acc
950+
where G: FnMut(Acc, Self::Item) -> Acc,
951+
{
952+
let mut f = self.f;
953+
self.iter.fold(init, move |acc, elt| g(acc, f(elt)))
954+
}
942955
}
943956

944957
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)