Skip to content

Commit

Permalink
WithPosition::fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Oct 3, 2023
1 parent 573d406 commit 0c2ffa7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/with_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ impl<I: Iterator> Iterator for WithPosition<I> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.peekable.size_hint()
}

fn fold<B, F>(mut self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
if let Some(mut head) = self.peekable.next() {
if !self.handled_first {
// The current head is `First` or `Only`,
// it depends if there is another item or not.
match self.peekable.next() {
Some(second) => {
let first = std::mem::replace(&mut head, second);
init = f(init, (Position::First, first));
}
None => return f(init, (Position::Only, head)),
}
}
// Have seen the first item, and there's something left.
init = self.peekable.fold(init, |acc, mut item| {
std::mem::swap(&mut head, &mut item);
f(acc, (Position::Middle, item))
});
// The "head" is now the last item.
init = f(init, (Position::Last, head));
}
init
}
}

impl<I> ExactSizeIterator for WithPosition<I> where I: ExactSizeIterator {}
Expand Down

0 comments on commit 0c2ffa7

Please sign in to comment.