3 ms·
Alternatively you can avoid the loop for the summing and just do. let diffsum: u64 = image1.raw_pixels().iter() .zip(image2.raw_pixels().iter())
by kevincox 7y ago
Alternatively you can avoid the loop for the summing and just do.
let diffsum: u64 = image1.raw_pixels().iter()
.zip(image2.raw_pixels().iter())
.map(|(&p1, &p2)| u64::from(abs_diff(p1, p2)))
.sum();
I find this a lot easier to read because there is no control flow in this snippet. I can see that it is just summing the diffs. I know some people prefer iterators and some prefer loops, however I think mixing them like was done often leads to less readable code.
- carlmr 7y agoI prefer iterators in concise places like this, but loops when there's a bit more logic inside.
- kevincox 7y agoDefinitely. If your logic flow naturally maps to common iterator methods I think it helps readability. If you need complex control flow it is best to use a loop then try to wrangle iterator methods to achieve it.