Skip to content

Commit 7f5f00d

Browse files
Interleave::fold
1 parent eeda182 commit 7f5f00d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/adaptors/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ 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 { mut a, mut b, flag } = self;
85+
if flag {
86+
match b.next() {
87+
Some(y) => init = f(init, y),
88+
None => return a.fold(init, f),
89+
}
90+
}
91+
let res = a.try_fold(init, |mut acc, x| {
92+
acc = f(acc, x);
93+
match b.next() {
94+
Some(y) => Ok(f(acc, y)),
95+
None => Err(acc),
96+
}
97+
});
98+
match res {
99+
Ok(acc) => b.fold(acc, f),
100+
Err(acc) => a.fold(acc, f),
101+
}
102+
}
79103
}
80104

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

0 commit comments

Comments
 (0)