3 ms·
Really? For loops are a nightmare. Here's one from SPEC 2006: for (dd=d[k=0]; k<16; dd=d[++k]) { satd += (dd < 0 ? -dd : dd); } The proble
by pcwalton 7y ago
Really? For loops are a nightmare. Here's one from SPEC 2006:
for (dd=d[k=0]; k<16; dd=d[++k])
{
satd += (dd < 0 ? -dd : dd);
}
The problem with for loops is that they are too flexible, and so there's more opportunity for misuse. Iterator functions are less flexible, and therefore easier to read.
And this excessive flexibility doesn't even make simple things simple. Try counting down from N to 0 inclusive using unsigned ints.
- tfha 7y agoLoops in go are much less flexible than loops in C, you'd never see code like that in go.
- fluffything 7y agoCan you solve the exact same problem using Python list comprehensions or Rust iterators? I'd like to see how solving this same problem in those languages is less of a nightmare. I personally find that C loop quite readable compared with the same iteration being done in Rust.
- pcwalton 7y agoThis is a sum of absolute differences, so in Rust: let satd = d.iter().cloned().map(abs).sum(); As an added bonus, there's no undefined behavior!