From 780acda325772b15f12f08f60ca2d4ba558cee51 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Tue, 25 Oct 2016 15:50:52 +0200 Subject: 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. --- src/libcore/iter/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/libcore') 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 fn size_hint(&self) -> (usize, Option) { self.it.size_hint() } + + fn fold(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 Iterator for Map where F: FnMut(I::Item) -> B { fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } + + fn fold(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")] -- cgit 1.4.1-3-g733a5 From a16626fc422f9fdcd1d02f56b628f764d5282261 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Tue, 25 Oct 2016 15:21:49 +0200 Subject: iter: Implement .fold() for .chain() Chain can do something interesting here where it passes on the fold into its inner iterators. The lets the underlying iterator's custom fold() be used, and skips the regular chain logic in next. --- src/libcore/iter/mod.rs | 19 +++++++++++++++++++ src/libcoretest/iter.rs | 12 ++++++++++++ 2 files changed, 31 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 2c3b8864a11..df4f5e5c576 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -550,6 +550,25 @@ impl Iterator for Chain where } } + fn fold(self, init: Acc, mut f: F) -> Acc + where F: FnMut(Acc, Self::Item) -> Acc, + { + let mut accum = init; + match self.state { + ChainState::Both | ChainState::Front => { + accum = self.a.fold(accum, &mut f); + } + _ => { } + } + match self.state { + ChainState::Both | ChainState::Back => { + accum = self.b.fold(accum, &mut f); + } + _ => { } + } + accum + } + #[inline] fn nth(&mut self, mut n: usize) -> Option { match self.state { diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 27eb25537f3..58b6444ef88 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -985,6 +985,18 @@ fn test_empty() { assert_eq!(it.next(), None); } +#[test] +fn test_chain_fold() { + let xs = [1, 2, 3]; + let ys = [1, 2, 0]; + + let mut iter = xs.iter().chain(&ys); + iter.next(); + let mut result = Vec::new(); + iter.fold((), |(), &elt| result.push(elt)); + assert_eq!(&[2, 3, 1, 2, 0], &result[..]); +} + #[bench] fn bench_rposition(b: &mut Bencher) { let it: Vec = (0..300).collect(); -- cgit 1.4.1-3-g733a5