diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index e925db51d..52ee4800c 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 @@ -1096,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