13 ms·
I find Python's list comprehensions unusable because they're backwards from how I'd expect. Thankfully in Scala the syntax is almost exactly the same; you write
by lmm 9y ago
I find Python's list comprehensions unusable because they're backwards from how I'd expect. Thankfully in Scala the syntax is almost exactly the same; you write
for {
a <- list1
b <- list2
} { do something}
for an imperative for loop, and
for {
a <- whatever1
b <- whatever2
} yield { ... }
for the general comprehension. In Python you have to put the equivalent of the yield right at the start, which is just so different from how everything else in the language works.
- vram22 9y agoThat's an interesting point I had not thought of before. For me, it does not make them unusable, but tastes differ. Also see my sibling comment.
- kazinator 9y agoWhoever designed the comprehension syntax for Python was perhaps enamored of the "such that" math notation { expr-with-vars | constraint-predicates }. https://en.wikipedia.org/wiki/Set-builder_notation#Sets_defined_by_a_predicate https://en.wikipedia.org/wiki/Set-builder_notation#Sets_defi...
- actsasbuffoon 9y agoThat's because Python's list comprehensions were derived from Haskell's implementation: https://wiki.python.org/moin/PythonVsHaskell#List_Comprehension_Syntax https://wiki.python.org/moin/PythonVsHaskell#List_Comprehens...:
- lmm 9y agoAs per https://wiki.haskell.org/List_comprehension https://wiki.haskell.org/List_comprehension , many Haskell users prefer to use do syntax (indeed I wasn't aware that the list comprehension syntax existed).
- actsasbuffoon 9y agoAgreed, I never use list comprehensions in Haskell. I feel like they're almost always harder to read.
- dmitriid 9y agoI wish they'd leave the math notation as is. Haskell's and Erlang's notations which keep it (maybe with slight modifications) are so much nicer and readable. Erlang [1]: [X || X <- [1,2,a,3,4,b,5,6], X > 3]. Pythagorean triplets [ {A,B,C} || A <- lists:seq(1,N), B <- lists:seq(1,N), C <- lists:seq(1,N), A+B+C =< N, A*A+B*B == C*C ]. [1] http://erlang.org/doc/programming_examples/list_comprehensions.html http://erlang.org/doc/programming_examples/list_comprehensio...