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 13, 2024
1 parent 824b2df commit 05e0acf
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ 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) => acc = f(acc, y),
None => return Err(acc),
}
Ok(acc)
});
match res {
Ok(val) => val,
Err(val) => val,
}
}
}

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

0 comments on commit 05e0acf

Please sign in to comment.