Skip to content

Commit

Permalink
RepeatN::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet authored and phimuemue committed Dec 15, 2023
1 parent 451c72a commit 7c5a5c0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/repeatn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ where
fn size_hint(&self) -> (usize, Option<usize>) {
(self.n, Some(self.n))
}

fn fold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
match self {
Self { elt: Some(elt), n } => {
debug_assert!(n > 0);
init = (1..n).map(|_| elt.clone()).fold(init, &mut f);
f(init, elt)
}
_ => init,
}
}
}

impl<A> DoubleEndedIterator for RepeatN<A>
Expand Down

0 comments on commit 7c5a5c0

Please sign in to comment.