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.
This commit is contained in:
Ulrik Sverdrup 2016-10-25 15:50:52 +02:00
parent 15a95866b4
commit 780acda325
1 changed files with 13 additions and 0 deletions

View File

@ -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")]