Skip to content

Commit 6c6af09

Browse files
committed
Add specialization fn fold to Coalesce (2)
1 parent d353c9f commit 6c6af09

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/adaptors/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,23 @@ impl<I, F> Iterator for Coalesce<I, F>
696696
fn size_hint(&self) -> (usize, Option<usize>) {
697697
self.iter.size_hint()
698698
}
699+
700+
fn fold<Acc, FnAcc>(self, acc: Acc, mut fn_acc: FnAcc) -> Acc
701+
where FnAcc: FnMut(Acc, Self::Item) -> Acc,
702+
{
703+
if let Some(last) = self.iter.last {
704+
let mut f = self.f;
705+
let (last, acc) = self.iter.iter.fold((last, acc), |(last, acc), elt| {
706+
match f(last, elt) {
707+
Ok(joined) => (joined, acc),
708+
Err((last_, next_)) => (next_, fn_acc(acc, last_)),
709+
}
710+
});
711+
fn_acc(acc, last)
712+
} else {
713+
acc
714+
}
715+
}
699716
}
700717

701718
/// An iterator adaptor that removes repeated duplicates, determining equality using a comparison function.

0 commit comments

Comments
 (0)