iterator - Iterate over pairs of chunks without creating a temporary vector -
i'm trying iterate vector pairs of chunks (in case it's image represented contiguous bitmap , i'd have access pixels 2 rows @ once).
the problem can't .chunks(w).chunks(2)
, have create temporary vector in between.
is there way purely iterators? (i'm ok if result iterator itself)
let input : vec<_> = (0..12).collect(); let tmp : vec<_> = input.chunks(3).collect(); let result : vec<_> = tmp.chunks(2).collect();
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
this solution create 2 iterators, 1 odd lines , 1 lines. combine 2 using .zip(), give iterator filled pairs:
fn main(){ let input : vec<_> = (0..12).collect(); let it1 = input.chunks(3).enumerate().filter_map(|x| if x.0 % 2 == 0 { some(x.1) } else { none }); let it2 = input.chunks(3).enumerate().filter_map(|x| if x.0 % 2 != 0 { some(x.1) } else { none }); let r: vec<_> = it1.zip(it2).collect(); println!("{:?}", r); }
Comments
Post a Comment