4 ms·
For what it's worth, I'll take for loops over list comprehensions any day of the week. I just find it a lot easier to read.
by nmfisher 6y ago
For what it's worth, I'll take for loops over list comprehensions any day of the week. I just find it a lot easier to read.
- random5634 6y agoAgreed! Check out pythons move with walrus operators inside list comprehensions - I think u can do that now - starts to be line noise
- simiones 6y agoSo you are claiming this arr2 := []int{} for i := range arr1 { if arr1[i] > 9 { arr2 = append(arr2, arr1[i] * 2) } } Is more readable than these? var arr2 = arr1.Where(x => x > 9).Select(x => x*2); var arr2 = from x in arr1 where x > 9 select x*2; arr2 = [x * 2 for x in arr1 where x > 9] (setf arr2 (loop for x across arr1 when (> x 9) collect (* x 2))) I honestly can't imagine by what measure any of the latter ones could be harder to understand the former. And as the complexity of the expression increases, I only see the advantage increasing typically (though often it pays to split it into multiple comprehensions, just like a loop that does too much).
- nmfisher 6y agoPersonally? Yes (except for the first example you gave, which I don’t classify as “list comprehension”). It’s not inherent complexity though, just personal preference. I grew up writing for loops so they’re second nature to me and I grok them instantly in a single pass, whereas some list comprehensions require a re-read (particularly if they’re nested). Different strokes, that’s all.
- simiones 6y agoFair enough!
- rswskg 6y agoIt's exactly this sort of comment that gives go lovers a bad name.