Skip to content

Commit 7c5a5c0

Browse files
Philippe-Choletphimuemue
authored andcommitted
RepeatN::fold
1 parent 451c72a commit 7c5a5c0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/repeatn.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ where
4444
fn size_hint(&self) -> (usize, Option<usize>) {
4545
(self.n, Some(self.n))
4646
}
47+
48+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
49+
where
50+
F: FnMut(B, Self::Item) -> B,
51+
{
52+
match self {
53+
Self { elt: Some(elt), n } => {
54+
debug_assert!(n > 0);
55+
init = (1..n).map(|_| elt.clone()).fold(init, &mut f);
56+
f(init, elt)
57+
}
58+
_ => init,
59+
}
60+
}
4761
}
4862

4963
impl<A> DoubleEndedIterator for RepeatN<A>

0 commit comments

Comments
 (0)