From 7c5a5c096393ec9500e0b59aa6d9eaf79cf7f672 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Sun, 26 Nov 2023 11:27:13 +0100 Subject: [PATCH] `RepeatN::fold` --- src/repeatn.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/repeatn.rs b/src/repeatn.rs index 512d057f3..15035ba00 100644 --- a/src/repeatn.rs +++ b/src/repeatn.rs @@ -44,6 +44,20 @@ where fn size_hint(&self) -> (usize, Option) { (self.n, Some(self.n)) } + + fn fold(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 DoubleEndedIterator for RepeatN