Created
April 17, 2023 04:45
-
-
Save orez-/e0d6bdce4c33ad46a5aac13f6425f70b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn interleave<I>(its: Vec<I>) -> Interleave<I> { | |
Interleave { its: its.into_iter().collect() } | |
} | |
struct Interleave<I> { | |
its: std::collections::VecDeque<I>, | |
} | |
impl<I: Iterator> Iterator for Interleave<I> { | |
type Item = I::Item; | |
fn next(&mut self) -> Option<I::Item> { | |
loop { | |
let mut it = self.its.pop_front()?; | |
if let Some(elem) = it.next() { | |
self.its.push_back(it); | |
return Some(elem); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment