3 ms·
So you are claiming this arr2 := []int{} for i := range arr1 { if arr1[i] > 9 { arr2 = append(arr2, arr1[i] * 2) } } Is more readable than
by simiones 6y ago
So 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!