Hacker News new | ask | show | jobs
by Larrikin 658 days ago
As someone who uses Kotlin for work and Python for side projects (and loved Python years ago in college), Python's list comprehension feature is one of the things I hate the most about the language now.

As a simple example using only two collection functions I find it much easier to read

  val hundredOrLessEvenSeconds = (1..1000)
      .toList()
      .filter { it <= 100 }
      .filter { it % 2 == 0 }
      .map { it.seconds }
than

  hundred_or_less_even_seconds = [timedelta(seconds=it) for it in range(1, 1001) if it <= 100 and it % 2 == 0]
But there are tons of helper functions in the collections library to express that in a variety of different ways. But not in a gross code golf way, with clearly named functions

Theres just so much built in https://kotlinlang.org/docs/collections-overview.html

Having lambdas built into the language from the start leads to a ton of expressibility I miss when using python

5 comments

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

Python's list comprehensions are close to set notation in math. Here's Python:

    [2*x for x in range(n + 1) if x <= 100]
Here's set notation:

    {2*x | x in 0..n, x <= 100}
So it reads pretty easily for those of us used to set notation. Haskell is even more similar to set notation:

    [2*x | x <- [0..n], x <= 100]
Gotta be real, I don't see any difference in readability (assuming it was formatted the same way, and honestly I'd prefer a different variable name than 'it' in both cases but I get that would require more boiler plate in kotlin and 'it' is a common invention).

The main difference imo is that kotlin uses more "syntax" while python uses more "English" to express the same thing. Also the half-open interval for range but that's an arbitrary decision that benefits some cases more than others (although my preference is the closed interval)

There is a sense in which Python uses more syntax, because list comprehensions are a special syntax.
You don't even need to create a range! You can just use:

    List(1000) { i -> i+1 }
Which creates a list of 1 to 1000

    import datetime
    import pandas as pd

    hundred_or_less_even_seconds = (
        pd.Series(range(1, 1000))
        .loc[lambda x: x <= 100]
        .loc[lambda x: x % 2 == 0]
        .map(lambda x: datetime.timedelta(seconds=x))
        .to_list()
    )
Frankly much less clearer/less readability than the Kotlin code.