Hacker News new | ask | show | jobs
by wk_end 654 days ago
Not that I especially want to defend Python, but can you elaborate a bit on why you find that chain easier to read? The Python version is straightforward enough - if it's just the absence of newlines you can write

  hundred_or_less_even_seconds = [
    timedelta(seconds=it)
    for it in range(1, 1001)
    if it <= 100 and it % 2 == 0
  ]
Also, I don't know Kotlin well enough, but is what you wrote going to be efficient? The Python version iterates once and creates one list (and you can actually turn it into a generator and make zero lists just by swapping the square brackets for parens); to my untrained eye, it looks like the Kotlin version is going to do more iteration and make four separate lists, three of which are just garbage to be thrown away immediately. Here that probably doesn't matter, but in other cases it might be a big problem; is there an easy/idiomatic way to avoid that?
5 comments

Its a small example so the efficiency doesn't matter, but you could use sequences when it does https://kotlinlang.org/docs/sequences.html .

Also the map function lets you perform any operations in it. It was a simple example but you could need to perform something slightly more complex than using another standard library function.

It's easier to compose functions in Kotlin. The python version you showed is more ad hoc, and is really one list comprehension.

I particularly like Kotlin's scope functions:

https://kotlinlang.org/docs/scope-functions.html

You can do partials in python too

I do prefer python over kotlin but IMHO semantically they're both beautiful and some of the least frictive languages I've ever used

> it looks like the Kotlin version is going to do more iteration and make four separate lists.

Someone with kotlin experience could comment too, But I don't think it creates four separate lists. The last map function iterates and asks for element from previous function which asks for element from its parent function. So there is only one list.

What if you want to filter on the mapped value in python? Or group by something and work further on the groups? It's almost unreadable after a few operations.
In that case I think the "Pythonic" thing to do would be to have some named intermediate steps:

  foos = (make_foo(bar) for bar in bars)
  acceptable_foos = (foo for foo in foos if acceptable(foo))
  ...
Depending on your circumstances, this may or may not be awkward (coming up with temporary names can be hard), or may or may not be a good idea anyway (naming things can help make the code more self-documenting). I can't reckon how it could become unreadable, per se - what do you mean by that?
That's kinda my point, though. It becomes either awkward or unreadable. You chose awkward in your solution. But simple things as mapping, sorting, filtering quickly becomes unwieldy. Either you have to make it lots of unnecessary steps, potentially also lots of function definitions because of the lack of proper lambdas, or you end up with filter(groupby(map(filter(...)))) trying to figure out what is going on.
What you’re doing, what you’re doing it, and any conditionals are all out of order. If you to translate the Python semantics into say, Rust syntax, you would have something akin to

.map(blah(x,y), |z|, if let (x,y) == conditional(z))