Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


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).


Personally? 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.


Fair enough!


Agreed! Check out pythons move with walrus operators inside list comprehensions - I think u can do that now - starts to be line noise


It's exactly this sort of comment that gives go lovers a bad name.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: