I need an idiomatic way to interlace these two vectors:
let v1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let v2 = vec![7.0, 8.0, 9.0, 10.0, 11.0, 12.0];
The output I expect is:
[1.0, 2.0, 3.0,
7.0, 8.0, 9.0,
4.0, 5.0, 6.0,
10.0, 11.0, 12.0];
I used itertools chunks
method, but I don't believe this is the best implementation because there are two collect
calls.
let output = interleave(&v1.into_iter().chunks(3), &v2.into_iter().chunks(3)).map(|v| {v.into_iter().collect::<Vec<f32>>()}).flatten().collect::<Vec<f32>>();
Is there a better way to write this iterator using itertools?
You want Iterator::flatten
:
use itertools::interleave;
fn main() {
let v1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let v2 = vec![7.0, 8.0, 9.0, 10.0, 11.0, 12.0];
let v = interleave(v1.chunks(3), v2.chunks(3))
.flatten()
.collect::<Vec<_>>();
dbg!(v);
}