Skip to content

Commit

Permalink
InterleaveShortest::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 16, 2024
1 parent 7f5f00d commit 98344ad
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,34 @@ where
};
(lower, upper)
}

fn fold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let Self {
mut it0,
mut it1,
phase,
} = self;
if phase {
match it1.next() {
Some(y) => init = f(init, y),
None => return init,
}
}
let res = it0.try_fold(init, |mut acc, x| {
acc = f(acc, x);
match it1.next() {
Some(y) => Ok(f(acc, y)),
None => Err(acc),
}
});
match res {
Ok(val) => val,
Err(val) => val,
}
}
}

impl<I, J> FusedIterator for InterleaveShortest<I, J>
Expand Down

0 comments on commit 98344ad

Please sign in to comment.