Skip to content

Commit 98344ad

Browse files
InterleaveShortest::fold
1 parent 7f5f00d commit 98344ad

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/adaptors/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,34 @@ where
195195
};
196196
(lower, upper)
197197
}
198+
199+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
200+
where
201+
F: FnMut(B, Self::Item) -> B,
202+
{
203+
let Self {
204+
mut it0,
205+
mut it1,
206+
phase,
207+
} = self;
208+
if phase {
209+
match it1.next() {
210+
Some(y) => init = f(init, y),
211+
None => return init,
212+
}
213+
}
214+
let res = it0.try_fold(init, |mut acc, x| {
215+
acc = f(acc, x);
216+
match it1.next() {
217+
Some(y) => Ok(f(acc, y)),
218+
None => Err(acc),
219+
}
220+
});
221+
match res {
222+
Ok(val) => val,
223+
Err(val) => val,
224+
}
225+
}
198226
}
199227

200228
impl<I, J> FusedIterator for InterleaveShortest<I, J>

0 commit comments

Comments
 (0)