Skip to content

Commit a3edd8d

Browse files
WithPosition::fold
1 parent 20611c8 commit a3edd8d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/with_position.rs

+27
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ impl<I: Iterator> Iterator for WithPosition<I> {
8181
fn size_hint(&self) -> (usize, Option<usize>) {
8282
self.peekable.size_hint()
8383
}
84+
85+
fn fold<B, F>(mut self, mut init: B, mut f: F) -> B
86+
where
87+
F: FnMut(B, Self::Item) -> B,
88+
{
89+
if !self.handled_first {
90+
// Haven't seen the first item yet, and there might be one to give.
91+
self.handled_first = true;
92+
if let Some(first) = self.peekable.next() {
93+
// Peek to see if this is also the last item,
94+
// in which case tag it as `Only`.
95+
match self.peekable.peek() {
96+
Some(_) => init = f(init, (Position::First, first)),
97+
None => return f(init, (Position::Only, first)),
98+
}
99+
}
100+
}
101+
if let Some(mut head) = self.peekable.next() {
102+
// Have seen the first item, and there's something left.
103+
(init, head) = self.peekable.fold((init, head), |(acc, old), new| {
104+
(f(acc, (Position::Middle, old)), new)
105+
});
106+
// The "head" is now the last item.
107+
init = f(init, (Position::Last, head));
108+
}
109+
init
110+
}
84111
}
85112

86113
impl<I> ExactSizeIterator for WithPosition<I> where I: ExactSizeIterator {}

0 commit comments

Comments
 (0)