Skip to content

Commit

Permalink
Merge pull request #30 from cuviper/rfold
Browse files Browse the repository at this point in the history
Implement and use DoubleEndedIterator::rfold
  • Loading branch information
cuviper authored Sep 7, 2022
2 parents f93531c + 3bd3360 commit a0f4ee0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,15 @@ where
fn next_back(&mut self) -> Option<I::Item> {
self.0.next_back().map(Clone::clone)
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.0.rfold(init, move |acc, item| f(acc, item.clone()))
}
}

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

None
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.rfold(init, move |acc, item| match map(item) {
Some(mapped) => f(acc, mapped),
None => acc,
})
}
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -1705,6 +1727,16 @@ where
fn next_back(&mut self) -> Option<Self::Item> {
self.it.next_back().map(&mut self.f)
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.rfold(init, move |acc, item| f(acc, map(item)))
}
}

/// A regular, non-streaming iterator which transforms the elements of a mutable streaming iterator.
Expand Down Expand Up @@ -1751,6 +1783,16 @@ where
fn next_back(&mut self) -> Option<Self::Item> {
self.it.next_back_mut().map(&mut self.f)
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
let mut map = self.f;
self.it.rfold_mut(init, move |acc, item| f(acc, map(item)))
}
}

/// A streaming iterator which transforms the elements of a streaming iterator.
Expand Down Expand Up @@ -1849,6 +1891,15 @@ where
fn next_back(&mut self) -> Option<<I::Item as ToOwned>::Owned> {
self.0.next_back().map(ToOwned::to_owned)
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.0.rfold(init, move |acc, item| f(acc, item.to_owned()))
}
}

/// A streaming iterator which skips a number of elements in a streaming iterator.
Expand Down
12 changes: 5 additions & 7 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
self.it.rev().fold(init, move |acc, item| f(acc, &item))
self.it.rfold(init, move |acc, item| f(acc, &item))
}
}

Expand Down Expand Up @@ -293,9 +293,7 @@ where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
self.it
.rev()
.fold(init, move |acc, mut item| f(acc, &mut item))
self.it.rfold(init, move |acc, mut item| f(acc, &mut item))
}
}

Expand Down Expand Up @@ -360,7 +358,7 @@ where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
self.it.rev().fold(init, f)
self.it.rfold(init, f)
}
}

Expand Down Expand Up @@ -428,7 +426,7 @@ where
Self: Sized,
Fold: FnMut(Acc, &Self::Item) -> Acc,
{
self.it.rev().fold(init, move |acc, item| f(acc, item))
self.it.rfold(init, move |acc, item| f(acc, item))
}
}

Expand Down Expand Up @@ -464,7 +462,7 @@ where
Self: Sized,
F: FnMut(B, &mut Self::Item) -> B,
{
self.it.rev().fold(init, f)
self.it.rfold(init, f)
}
}

Expand Down

0 comments on commit a0f4ee0

Please sign in to comment.