4 ms·
Doing good Python would help. You don't need the backslash on the first, making it a little cleaner: map(lambda x: x * 2, filter(lambda x: x % 2 ==
by SoftwareMaven 11y ago
Doing good Python would help. You don't need the backslash on the first, making it a little cleaner:
map(lambda x: x * 2,
filter(lambda x: x % 2 == 0, [1, 2, 3]))
But the list comprehension version is just completely wrong:
[x*2 for x in [1, 2, 3] if x % 2 == 0]
Which, in our subjective world, is cleaner than the ES5 version and almost as clean as the ES6 version.
- nilliams 11y ago> You don't need the backslash on the first, making it a little cleaner I was not trying to illustrate that the backslash made it ugly (I've removed it - I was pasting from a repl). It's ugly because of Python's lambda syntax and the fact it does not compose 'sensibly', as compared to fluid interfaces. The logic is back-to-front for one. > But the list comprehension version is just completely wrong: No, it's not at all wrong. Your one-liner is not better and (I hope) would probably upset most teams for being less clear. I was providing a like-for-like example. The multi-lining is not the criticism of the Python version. It's the composition that's at fault, the fact it does not feel like 'two coherent operations over a list' with two distinct conditionals. The ES5/ES6 examples demonstrate this much more clearly IMO.
- 59nadir 11y agoWhat you're saying, then, is that it's terribly confusing [for you] when someone uses a list comprehension fully? Presumably, these also are not completely clear to you: > [x * 2 | x <- [1, 2, 3, 4], even x] [4,8] > [x * 2 | x <- filter even [1, 2, 3, 4]] [4,8] What, exactly, is not clear here? This is the essence of composition; to use clearly defined things together. I can appreciate a good pipeline as much as the next guy, but to say that this is somehow more complicated and hard to read is to overstretch a little bit. To say that a pretty uncluttered list comprehension is some challenge to read is just hyperbolic. These pipelines, even with newlines, can get pretty long and terrible too.