Skip to content

Commit 31cf26a

Browse files
committed
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.
1 parent 91318c8 commit 31cf26a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libcore/iter/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
359359
#[inline]
360360
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
361361

362+
fn fold<Acc, F>(self, init: Acc, f: F) -> Acc
363+
where F: FnMut(Acc, Self::Item) -> Acc,
364+
{
365+
self.iter.rfold(init, f)
366+
}
367+
362368
#[inline]
363369
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
364370
where P: FnMut(&Self::Item) -> bool
@@ -379,6 +385,12 @@ impl<I> DoubleEndedIterator for Rev<I> where I: DoubleEndedIterator {
379385
#[inline]
380386
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
381387

388+
fn rfold<Acc, F>(self, init: Acc, f: F) -> Acc
389+
where F: FnMut(Acc, Self::Item) -> Acc,
390+
{
391+
self.iter.fold(init, f)
392+
}
393+
382394
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
383395
where P: FnMut(&Self::Item) -> bool
384396
{

0 commit comments

Comments
 (0)