From 31cf26a953fe69c23de49f98676a746fea5dcefb Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Mon, 18 Sep 2017 21:24:33 +0200 Subject: [PATCH] core: Implement fold / rfold for Rev With both in place, we can cross them over in rev, and we give rfold behaviour to .rev().fold() and so on. --- src/libcore/iter/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index ebedfe1d743b..65cd28b499e1 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -359,6 +359,12 @@ impl Iterator for Rev where I: DoubleEndedIterator { #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } + fn fold(self, init: Acc, f: F) -> Acc + where F: FnMut(Acc, Self::Item) -> Acc, + { + self.iter.rfold(init, f) + } + #[inline] fn find

(&mut self, predicate: P) -> Option where P: FnMut(&Self::Item) -> bool @@ -379,6 +385,12 @@ impl DoubleEndedIterator for Rev where I: DoubleEndedIterator { #[inline] fn next_back(&mut self) -> Option<::Item> { self.iter.next() } + fn rfold(self, init: Acc, f: F) -> Acc + where F: FnMut(Acc, Self::Item) -> Acc, + { + self.iter.fold(init, f) + } + fn rfind

(&mut self, predicate: P) -> Option where P: FnMut(&Self::Item) -> bool {