From 8cb4f00733b702bd54fff29c479bcc706d083d5e Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Wed, 8 Nov 2023 12:11:06 +0100 Subject: [PATCH 1/2] `Positions::fold` --- src/adaptors/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index e925db51d..7f6ff95a1 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -1081,6 +1081,21 @@ where fn size_hint(&self) -> (usize, Option) { (0, self.iter.size_hint().1) } + + fn fold(self, init: B, mut func: G) -> B + where + G: FnMut(B, Self::Item) -> B, + { + let mut count = self.count; + let mut f = self.f; + self.iter.fold(init, |mut acc, val| { + if f(val) { + acc = func(acc, count); + } + count += 1; + acc + }) + } } impl DoubleEndedIterator for Positions From d0ee7e071c129a50b2d0bc336132a31012544ba3 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Fri, 24 Nov 2023 10:47:02 +0100 Subject: [PATCH 2/2] `Positions::rfold` --- src/adaptors/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 7f6ff95a1..52ee4800c 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -1111,6 +1111,21 @@ where } None } + + fn rfold(self, init: B, mut func: G) -> B + where + G: FnMut(B, Self::Item) -> B, + { + let mut count = self.count + self.iter.len(); + let mut f = self.f; + self.iter.rfold(init, |mut acc, val| { + count -= 1; + if f(val) { + acc = func(acc, count); + } + acc + }) + } } impl FusedIterator for Positions