Skip to content

Commit bb31180

Browse files
committed
Implement FlattenOk::fold
1 parent dd6a569 commit bb31180

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/flatten_ok.rs

+23
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ where
7272
}
7373
}
7474

75+
fn fold<B, F>(self, init: B, mut f: F) -> B
76+
where
77+
Self: Sized,
78+
F: FnMut(B, Self::Item) -> B,
79+
{
80+
// Front
81+
let mut acc = match self.inner_front {
82+
Some(x) => x.fold(init, |a, o| f(a, Ok(o))),
83+
None => init,
84+
};
85+
86+
acc = self.iter.fold(acc, |acc, x| match x {
87+
Ok(it) => it.into_iter().fold(acc, |a, o| f(a, Ok(o))),
88+
Err(e) => f(acc, Err(e)),
89+
});
90+
91+
// Back
92+
match self.inner_back {
93+
Some(x) => x.fold(acc, |a, o| f(a, Ok(o))),
94+
None => acc,
95+
}
96+
}
97+
7598
fn size_hint(&self) -> (usize, Option<usize>) {
7699
let inner_hint = |inner: &Option<T::IntoIter>| {
77100
inner

0 commit comments

Comments
 (0)