Skip to content

Commit

Permalink
Implement FlattenOk::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
kinto-b committed May 1, 2024
1 parent dd6a569 commit bb31180
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/flatten_ok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ where
}
}

fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
// Front
let mut acc = match self.inner_front {
Some(x) => x.fold(init, |a, o| f(a, Ok(o))),
None => init,
};

acc = self.iter.fold(acc, |acc, x| match x {
Ok(it) => it.into_iter().fold(acc, |a, o| f(a, Ok(o))),
Err(e) => f(acc, Err(e)),
});

// Back
match self.inner_back {
Some(x) => x.fold(acc, |a, o| f(a, Ok(o))),
None => acc,
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let inner_hint = |inner: &Option<T::IntoIter>| {
inner
Expand Down

0 comments on commit bb31180

Please sign in to comment.