Skip to content

Commit

Permalink
Interleave::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 16, 2024
1 parent eeda182 commit 7f5f00d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ where
fn size_hint(&self) -> (usize, Option<usize>) {
size_hint::add(self.a.size_hint(), self.b.size_hint())
}

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

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

0 comments on commit 7f5f00d

Please sign in to comment.