Skip to content

Commit 05e0acf

Browse files
InterleaveShortest::fold
1 parent 824b2df commit 05e0acf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/adaptors/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ where
193193
};
194194
(lower, upper)
195195
}
196+
197+
fn fold<B, F>(self, mut init: B, mut f: F) -> B
198+
where
199+
F: FnMut(B, Self::Item) -> B,
200+
{
201+
let Self {
202+
mut it0,
203+
mut it1,
204+
phase,
205+
} = self;
206+
if phase {
207+
match it1.next() {
208+
Some(y) => init = f(init, y),
209+
None => return init,
210+
}
211+
}
212+
let res = it0.try_fold(init, |mut acc, x| {
213+
acc = f(acc, x);
214+
match it1.next() {
215+
Some(y) => acc = f(acc, y),
216+
None => return Err(acc),
217+
}
218+
Ok(acc)
219+
});
220+
match res {
221+
Ok(val) => val,
222+
Err(val) => val,
223+
}
224+
}
196225
}
197226

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

0 commit comments

Comments
 (0)