Skip to content

Commit a59a25d

Browse files
committed
core: Implement rfold for Map, Cloned, Chain
1 parent 31cf26a commit a59a25d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/libcore/iter/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
461461
fn next_back(&mut self) -> Option<T> {
462462
self.it.next_back().cloned()
463463
}
464+
465+
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
466+
where F: FnMut(Acc, Self::Item) -> Acc,
467+
{
468+
self.it.rfold(init, move |acc, elt| f(acc, elt.clone()))
469+
}
464470
}
465471

466472
#[stable(feature = "iter_cloned", since = "1.1.0")]
@@ -773,6 +779,26 @@ impl<A, B> DoubleEndedIterator for Chain<A, B> where
773779
ChainState::Back => self.b.next_back(),
774780
}
775781
}
782+
783+
fn rfold<Acc, F>(self, init: Acc, mut f: F) -> Acc
784+
where F: FnMut(Acc, Self::Item) -> Acc,
785+
{
786+
let mut accum = init;
787+
match self.state {
788+
ChainState::Both | ChainState::Back => {
789+
accum = self.b.rfold(accum, &mut f);
790+
}
791+
_ => { }
792+
}
793+
match self.state {
794+
ChainState::Both | ChainState::Front => {
795+
accum = self.a.rfold(accum, &mut f);
796+
}
797+
_ => { }
798+
}
799+
accum
800+
}
801+
776802
}
777803

778804
// Note: *both* must be fused to handle double-ended iterators.
@@ -1106,6 +1132,13 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for Map<I, F> where
11061132
fn next_back(&mut self) -> Option<B> {
11071133
self.iter.next_back().map(&mut self.f)
11081134
}
1135+
1136+
fn rfold<Acc, G>(self, init: Acc, mut g: G) -> Acc
1137+
where G: FnMut(Acc, Self::Item) -> Acc,
1138+
{
1139+
let mut f = self.f;
1140+
self.iter.rfold(init, move |acc, elt| g(acc, f(elt)))
1141+
}
11091142
}
11101143

11111144
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)