Skip to content

Commit 33afea5

Browse files
RepeatN::fold
1 parent 451c72a commit 33afea5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/repeatn.rs

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ 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 {
54+
elt: Some(elt),
55+
n: n @ 1..,
56+
} => {
57+
init = (1..n).map(|_| elt.clone()).fold(init, &mut f);
58+
f(init, elt)
59+
}
60+
_ => init,
61+
}
62+
}
4763
}
4864

4965
impl<A> DoubleEndedIterator for RepeatN<A>

0 commit comments

Comments
 (0)