Skip to content

Commit a0f4ee0

Browse files
authored
Merge pull request #30 from cuviper/rfold
Implement and use DoubleEndedIterator::rfold
2 parents f93531c + 3bd3360 commit a0f4ee0

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,15 @@ where
884884
fn next_back(&mut self) -> Option<I::Item> {
885885
self.0.next_back().map(Clone::clone)
886886
}
887+
888+
#[inline]
889+
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
890+
where
891+
Self: Sized,
892+
Fold: FnMut(Acc, Self::Item) -> Acc,
893+
{
894+
self.0.rfold(init, move |acc, item| f(acc, item.clone()))
895+
}
887896
}
888897

889898
/// A streaming iterator which filters the elements of a streaming iterator with a predicate.
@@ -1313,6 +1322,19 @@ where
13131322

13141323
None
13151324
}
1325+
1326+
#[inline]
1327+
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1328+
where
1329+
Self: Sized,
1330+
Fold: FnMut(Acc, Self::Item) -> Acc,
1331+
{
1332+
let mut map = self.f;
1333+
self.it.rfold(init, move |acc, item| match map(item) {
1334+
Some(mapped) => f(acc, mapped),
1335+
None => acc,
1336+
})
1337+
}
13161338
}
13171339

13181340
#[derive(Copy, Clone, Debug)]
@@ -1705,6 +1727,16 @@ where
17051727
fn next_back(&mut self) -> Option<Self::Item> {
17061728
self.it.next_back().map(&mut self.f)
17071729
}
1730+
1731+
#[inline]
1732+
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1733+
where
1734+
Self: Sized,
1735+
Fold: FnMut(Acc, Self::Item) -> Acc,
1736+
{
1737+
let mut map = self.f;
1738+
self.it.rfold(init, move |acc, item| f(acc, map(item)))
1739+
}
17081740
}
17091741

17101742
/// A regular, non-streaming iterator which transforms the elements of a mutable streaming iterator.
@@ -1751,6 +1783,16 @@ where
17511783
fn next_back(&mut self) -> Option<Self::Item> {
17521784
self.it.next_back_mut().map(&mut self.f)
17531785
}
1786+
1787+
#[inline]
1788+
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1789+
where
1790+
Self: Sized,
1791+
Fold: FnMut(Acc, Self::Item) -> Acc,
1792+
{
1793+
let mut map = self.f;
1794+
self.it.rfold_mut(init, move |acc, item| f(acc, map(item)))
1795+
}
17541796
}
17551797

17561798
/// A streaming iterator which transforms the elements of a streaming iterator.
@@ -1849,6 +1891,15 @@ where
18491891
fn next_back(&mut self) -> Option<<I::Item as ToOwned>::Owned> {
18501892
self.0.next_back().map(ToOwned::to_owned)
18511893
}
1894+
1895+
#[inline]
1896+
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1897+
where
1898+
Self: Sized,
1899+
Fold: FnMut(Acc, Self::Item) -> Acc,
1900+
{
1901+
self.0.rfold(init, move |acc, item| f(acc, item.to_owned()))
1902+
}
18521903
}
18531904

18541905
/// A streaming iterator which skips a number of elements in a streaming iterator.

src/sources.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ where
260260
Self: Sized,
261261
Fold: FnMut(Acc, &Self::Item) -> Acc,
262262
{
263-
self.it.rev().fold(init, move |acc, item| f(acc, &item))
263+
self.it.rfold(init, move |acc, item| f(acc, &item))
264264
}
265265
}
266266

@@ -293,9 +293,7 @@ where
293293
Self: Sized,
294294
F: FnMut(B, &mut Self::Item) -> B,
295295
{
296-
self.it
297-
.rev()
298-
.fold(init, move |acc, mut item| f(acc, &mut item))
296+
self.it.rfold(init, move |acc, mut item| f(acc, &mut item))
299297
}
300298
}
301299

@@ -360,7 +358,7 @@ where
360358
Self: Sized,
361359
Fold: FnMut(Acc, &Self::Item) -> Acc,
362360
{
363-
self.it.rev().fold(init, f)
361+
self.it.rfold(init, f)
364362
}
365363
}
366364

@@ -428,7 +426,7 @@ where
428426
Self: Sized,
429427
Fold: FnMut(Acc, &Self::Item) -> Acc,
430428
{
431-
self.it.rev().fold(init, move |acc, item| f(acc, item))
429+
self.it.rfold(init, move |acc, item| f(acc, item))
432430
}
433431
}
434432

@@ -464,7 +462,7 @@ where
464462
Self: Sized,
465463
F: FnMut(B, &mut Self::Item) -> B,
466464
{
467-
self.it.rev().fold(init, f)
465+
self.it.rfold(init, f)
468466
}
469467
}
470468

0 commit comments

Comments
 (0)