Skip to content

Commit 824b2df

Browse files
Interleave::fold
1 parent 5a321ca commit 824b2df

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/adaptors/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ where
7676
fn size_hint(&self) -> (usize, Option<usize>) {
7777
size_hint::add(self.a.size_hint(), self.b.size_hint())
7878
}
79+
80+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
81+
where
82+
F: FnMut(B, Self::Item) -> B,
83+
{
84+
let Self { a, mut b, flag } = self;
85+
if flag {
86+
if let Some(y) = b.next() {
87+
init = f(init, y);
88+
} else {
89+
return a.fold(init, f);
90+
}
91+
}
92+
init = a.fold(init, |mut acc, x| {
93+
acc = f(acc, x);
94+
if let Some(y) = b.next() {
95+
acc = f(acc, y);
96+
}
97+
acc
98+
});
99+
b.fold(init, f)
100+
}
79101
}
80102

81103
impl<I, J> FusedIterator for Interleave<I, J>

0 commit comments

Comments
 (0)