7 ms·
While the solution is composed neatly from functional primitives, the large amount of boxes of boxes are not ideal. For example, Interleave stores a Vec<Box<It
by Arnavion 7y ago
While the solution is composed neatly from functional primitives, the large amount of boxes of boxes are not ideal.
For example, Interleave stores a Vec<Box<Iterator>>, which it absolutely should for the general case. But Flatten only ever uses it with two iterators, so the Vec is an extra allocation that could've been avoided in Flatten's case.
For the specific case of flattening an infinite iterator of infinite iterators, once could simplify to:
struct FlattenInfinite<I> where I: IntoIterator, <I as IntoIterator>::Item: IntoIterator {
iters: <I as IntoIterator>::IntoIter,
realized_iters: Vec<<I::Item as IntoIterator>::IntoIter>,
cursor: usize,
}
where the number of trailing ones in self.cursor specifies which iterator to yield the next element from.
Full example at https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=b2c807cc5537fa2915fe11855bf44373 https://play.rust-lang.org/?version=stable&mode=release&edit...
The `.expect()`s require that the solution is specific to an infinite iterator of infinite iterators.
(Edit: Better implementation that uses usize::trailing_zeros() instead of hand-counting trailing ones.)