Add some more rfold
implementations.
This commit is contained in:
parent
c2abf8f9c3
commit
959bd48887
@ -724,6 +724,29 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rfold<Acc, F>(mut self, init: Acc, mut f: F) -> Acc
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnMut(Acc, Self::Item) -> Acc,
|
||||
{
|
||||
#[inline]
|
||||
fn nth_back<I: DoubleEndedIterator>(
|
||||
iter: &mut I,
|
||||
step: usize,
|
||||
) -> impl FnMut() -> Option<I::Item> + '_ {
|
||||
move || iter.nth_back(step)
|
||||
}
|
||||
|
||||
match self.next_back() {
|
||||
None => init,
|
||||
Some(x) => {
|
||||
let acc = f(init, x);
|
||||
from_fn(nth_back(&mut self.iter, self.step)).fold(acc, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// StepBy can only make the iterator shorter, so the len will still fit.
|
||||
@ -2056,6 +2079,18 @@ where
|
||||
self.iter.try_rfold(init, check(n, fold)).into_try()
|
||||
}
|
||||
}
|
||||
|
||||
fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
|
||||
where
|
||||
Fold: FnMut(Acc, Self::Item) -> Acc,
|
||||
{
|
||||
#[inline]
|
||||
fn ok<Acc, T>(mut f: impl FnMut(Acc, T) -> Acc) -> impl FnMut(Acc, T) -> Result<Acc, !> {
|
||||
move |acc, x| Ok(f(acc, x))
|
||||
}
|
||||
|
||||
self.try_rfold(init, ok(fold)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "fused", since = "1.26.0")]
|
||||
@ -2220,6 +2255,24 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rfold<Acc, Fold>(mut self, init: Acc, fold: Fold) -> Acc
|
||||
where
|
||||
Self: Sized,
|
||||
Fold: FnMut(Acc, Self::Item) -> Acc,
|
||||
{
|
||||
if self.n == 0 {
|
||||
init
|
||||
} else {
|
||||
let len = self.iter.len();
|
||||
if len > self.n && self.iter.nth_back(len - self.n - 1).is_none() {
|
||||
init
|
||||
} else {
|
||||
self.iter.rfold(init, fold)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
@ -671,6 +671,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
|
||||
|
||||
self.try_fold(init, ok(f)).unwrap()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn last(mut self) -> Option<A> {
|
||||
self.next_back()
|
||||
@ -759,6 +760,20 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
|
||||
|
||||
Try::from_ok(accum)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rfold<B, F>(mut self, init: B, f: F) -> B
|
||||
where
|
||||
Self: Sized,
|
||||
F: FnMut(B, Self::Item) -> B,
|
||||
{
|
||||
#[inline]
|
||||
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
|
||||
move |acc, x| Ok(f(acc, x))
|
||||
}
|
||||
|
||||
self.try_rfold(init, ok(f)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "trusted_len", issue = "37572")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user