not that python doesn't allow to do stuff like tha...
# random
b
not that python doesn't allow to do stuff like that: sorted(filter(lambda x: 'foo' in x, list_), key=operator.attrgetter('intensity'))
d
list comprehensions are nicer
😄 1
👎 1
👍 1
k
Curious why you like list comprehensions better, @Dias. Every time I use Python I'm so confused by why their solution to collection transformations is so ugly. The ordering doesn't make any sense to me:
Copy code
len(name) for name in names
is just all over the place. You have to read the end of the line to understand what's going on at the front. Meanwhile, in Kotlin it's just:
Copy code
names.map { name -> name.length }
and I feel like at no point in reading that line are you surprised by a variable you haven't seen at all in the past
and it gets even more confusing when you add a filter case:
Copy code
len(name) for name in names if len(name) % 2 == 0
it feels so weird that the subject variables are squished in the middle of the transform (on the left) and the predicate (on the right)
Copy code
names.map { name -> name.length }.filter { name -> name.length % 2 == 0 }
reads sequentially, again
and doing it as functions instead of as a comprehension is more flexible if you wanted to do other operations (
sumBy
,
sorted
,
reduce
, etc), and lets you define your own operations that fit into the chain more fluently
d
ok, it’s a personal preference thing. But I wasn’t comparing to kotlin. I was comparing to python comprehensions to python functions calls
[x*3 for x in list if x %2 == 0]
looks a lot nicer than
list(map(lambda x: x*3, filter(lambda f: f%2==0, list)))
and yeah I like “fluent” style like in kotlin or java more than any of those
👍 1
k
ok, agreed with you on all that 🙂