diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-25 15:21:49 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-25 22:06:39 +0200 |
| commit | a16626fc422f9fdcd1d02f56b628f764d5282261 (patch) | |
| tree | 2906da152b755dd46bef34a1f9361b75128650a4 /src/libcore | |
| parent | 780acda325772b15f12f08f60ca2d4ba558cee51 (diff) | |
| download | rust-a16626fc422f9fdcd1d02f56b628f764d5282261.tar.gz rust-a16626fc422f9fdcd1d02f56b628f764d5282261.zip | |
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.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter/mod.rs | 19 |
1 files changed, 19 insertions, 0 deletions
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<A, B> Iterator for Chain<A, B> where } } + fn fold<Acc, F>(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<A::Item> { match self.state { |
