From fc5aab4d9f4880480ba0ef9da14674be0d432600 Mon Sep 17 00:00:00 2001 From: Kinto Date: Wed, 1 May 2024 21:26:35 +1000 Subject: [PATCH] Implement `FlattenOk::rfold` --- src/flatten_ok.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/flatten_ok.rs b/src/flatten_ok.rs index 257ef70ae..48f1e90a6 100644 --- a/src/flatten_ok.rs +++ b/src/flatten_ok.rs @@ -153,6 +153,29 @@ where } } } + + fn rfold(self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + // Back + let mut acc = match self.inner_back { + Some(x) => x.rfold(init, |a, o| f(a, Ok(o))), + None => init, + }; + + acc = self.iter.rfold(acc, |acc, x| match x { + Ok(it) => it.into_iter().rfold(acc, |a, o| f(a, Ok(o))), + Err(e) => f(acc, Err(e)), + }); + + // Front + match self.inner_front { + Some(x) => x.rfold(acc, |a, o| f(a, Ok(o))), + None => acc, + } + } } impl Clone for FlattenOk