News for hackers. Stuff that matters.
Re:Kotlin for Data Analysis by Larrikin on Wednesday August 28, 2024 @08:05PM (#41385768)
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

Re:Kotlin for Data Analysis by wk_end on Wednesday August 28, 2024 @08:57PM (#41386145)
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?
Re:Kotlin for Data Analysis by Larrikin on Wednesday August 28, 2024 @09:10PM (#41386218)
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.

Re:Kotlin for Data Analysis by esafak on Wednesday August 28, 2024 @09:01PM (#41386168)
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

Re:Kotlin for Data Analysis by hughesjj on Wednesday August 28, 2024 @09:33PM (#41386352)
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

Re:Kotlin for Data Analysis by FridgeSeal on Wednesday August 28, 2024 @09:14PM (#41386242)
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))

Re:Kotlin for Data Analysis by matsemann on Thursday August 29, 2024 @06:24AM (#41389240)
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.
Re:Kotlin for Data Analysis by wk_end on Thursday August 29, 2024 @02:39PM (#41393982)
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?
Re:Kotlin for Data Analysis by matsemann on Friday August 30, 2024 @04:01AM (#41398636)
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.
Re:Kotlin for Data Analysis by boruto on Friday August 30, 2024 @02:34AM (#41398222)
> 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.

Re:Kotlin for Data Analysis by sampo on Wednesday August 28, 2024 @09:39PM (#41386386)

    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()
    )
Re:Kotlin for Data Analysis by billfruit on Thursday August 29, 2024 @01:02AM (#41387512)
Frankly much less clearer/less readability than the Kotlin code.
Re:Kotlin for Data Analysis by hughesjj on Wednesday August 28, 2024 @09:41PM (#41386407)
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)

Re:Kotlin for Data Analysis by solidsnack9000 on Wednesday August 28, 2024 @11:03PM (#41386845)
There is a sense in which Python uses more syntax, because list comprehensions are a special syntax.
Re:Kotlin for Data Analysis by wenc on Wednesday August 28, 2024 @11:22PM (#41386960)
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]
Re:Kotlin for Data Analysis by yuye on Thursday August 29, 2024 @03:22AM (#41388217)
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