Skip to content

Commit

Permalink
ZipLongest::fold: use try_fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 18, 2024
1 parent 98344ad commit 6c588cd
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/zip_longest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ where
}

#[inline]
fn fold<B, F>(self, mut init: B, mut f: F) -> B
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let Self { a, mut b } = self;
init = a.fold(init, |init, a| match b.next() {
Some(b) => f(init, EitherOrBoth::Both(a, b)),
None => f(init, EitherOrBoth::Left(a)),
let Self { mut a, mut b } = self;
let res = a.try_fold(init, |init, a| match b.next() {
Some(b) => Ok(f(init, EitherOrBoth::Both(a, b))),
None => Err(f(init, EitherOrBoth::Left(a))),
});
b.fold(init, |init, b| f(init, EitherOrBoth::Right(b)))
match res {
Ok(acc) => b.map(EitherOrBoth::Right).fold(acc, f),
Err(acc) => a.map(EitherOrBoth::Left).fold(acc, f),
}
}
}

Expand Down

0 comments on commit 6c588cd

Please sign in to comment.