Live data from Hacker News

Kotlin for data analysis

kotlinlang.org

41–50 of 105 posts

Re: Kotlin for data analysis

#41

I do both Kotlin and Python. More Kotlin than Python to be honest. But I'm pragmatic. Python is where all the action is when it comes to data science, llms, and all the rest. So it's the path of the least resistance. And there's a great argument to not challenge that and just do what everybody else does and put your head down and not criticize any of that. Which is why I use it on a few projects. The library ecosyste…

> The interpreter is not that fast, the language is not that expressive, what passes for package management is a bad joke, etc. I can work with it but I'm not necessarily loving it.

These are rather disingenuous criticisms of python. The utility of any language is measured by its use as a means to some end. It's clear that Python's ecosystem has prospered not just because it's a fad, but because it's actually useful. Not perfect, but useful.

Python can be improved, but I'm sure most other tools/languages can be equally criticized.

Re: Kotlin for data analysis

#42

Earlier quoted context omitted.

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 than hundred_or_less_even_seconds = [timedelta(seconds=it) for it i…

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 bu…

There is a sense in which Python uses more syntax, because list comprehensions are a special syntax.

Re: Kotlin for data analysis

#43

I do both Kotlin and Python. More Kotlin than Python to be honest. But I'm pragmatic. Python is where all the action is when it comes to data science, llms, and all the rest. So it's the path of the least resistance. And there's a great argument to not challenge that and just do what everybody else does and put your head down and not criticize any of that. Which is why I use it on a few projects. The library ecosyste…

I would love to have an excuse to spend most of my time in Kotlin but unfortunately it's not a good fit for the apps I build. It's really a lovely language.

Maybe if Kotlin Multiplatform really takes off.

Re: Kotlin for data analysis

#44

I do both Kotlin and Python. More Kotlin than Python to be honest. But I'm pragmatic. Python is where all the action is when it comes to data science, llms, and all the rest. So it's the path of the least resistance. And there's a great argument to not challenge that and just do what everybody else does and put your head down and not criticize any of that. Which is why I use it on a few projects. The library ecosyste…

I think both languages have their strengths. I love Kotlin for its functional programming (map, filter, etc) and strong static typing. But Python has some nice features as well, such as list comprehension, the 'yield' keyword, and annotations are super simple to implement.

> the 'yield' keyword

Am I missing something here?

  $ cat fib.kts
  fun fib() = sequence {
    var a = 1; var b = 1
    while (true) {
      yield(a)
      a = b.also { b += a }
    }
  }

  println(fib().take(10).joinToString(" -> "))
  println(fib().first { it >= 50 })
  $ kotlin fib.kts
  1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13 -> 21 -> 34 -> 55
  55
Of course, yield() is a function in Kotlin, not a keyword, but the same functionality is there.

Re: Kotlin for data analysis

#45

Earlier quoted context omitted.

Are there things that you find easier to express in a list comprehension format than you do with Kotlin's standard library? I've always found comprehensions to be a bit of a workaround to the fact that Python doesn't have great support for anonymous functions, and I've never found a situation where I'm writing Kotlin really wishing I had a comprehension.

A bit of a contrived example, but something like this (two for statements): [x*y for x in range (10) for y in range(10)] It's not often, but occasionally there are moments where I'm writing code in Kotlin and wish I could use a list comprehension. I do prefer Kotlin overall, but there's a few things that I think would be "nice to have" from Python. Especially the yield keyword, such a wonderful way to write your own…

Like this?

  sequence { for (x in 0..
Now, technically, Kotlin doesn't have list comprehensions, only the equivalent of generator expressions in Python, so you have to tack an extra `.toList()` on at the end if you want a list, but you can write pretty much any for comprehension in Python in a similar way in Kotlin.

On the other hand, you're not limited to for loops/ifs inside such a generator, but can use fairly arbitrary control flow.

Re: Kotlin for data analysis

#46

Earlier quoted context omitted.

I'm not sure how someone could see Kotlin as more expressive than Python, unless I am misinterpreting what expressive means. Python has a good language features and helpful abstractions like list comprehensions. What makes Kotlin more expressive? I understand it has some functional features but I've never seen anything dramatically flexible.

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 than hundred_or_less_even_seconds = [timedelta(seconds=it) for it i…

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

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

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

    [2*x | x 

Re: Kotlin for data analysis

#47

Earlier quoted context omitted.

Are there things that you find easier to express in a list comprehension format than you do with Kotlin's standard library? I've always found comprehensions to be a bit of a workaround to the fact that Python doesn't have great support for anonymous functions, and I've never found a situation where I'm writing Kotlin really wishing I had a comprehension.

I’ve always liked that the structure of list comprehensions mirror SQL with “SELECT, FROM, WHERE” sections. But I know that I’m in the minority that likes SQL

They remind me of C#'s LINQ

Re: Kotlin for data analysis

#48

Earlier quoted context omitted.

Hmm, I actually prefer Kotlin's version, but maybe that's just because it's what I'm used to: (0..10).flatMap { x -> (0..10).map { y -> x * y } } The flow of the data is more intuitive for me because you don't use variables before they're defined. > Especially the yield keyword, such a wonderful way to write your own iterators. Maybe I'm missing something about `yield` in Python—can it do something that Kotlin genera…

It's mostly ease of semantics -- in your example you use two layers of map and as a result need to do flatMap instead of just map twice In py, for list/set/dictionary/generator comprehensions, the format is always the same and always the same as if you were to do it as a normal nested loop, save for the statement being first instead of last (you can also do filters using normal if statement syntax, these go at the en…

Theres the zip function builtin, which I actually would have preferred

  oneToTen.zip(oneToTen2) { x, y -> x * y }

Re: Kotlin for data analysis

#49
post #7

I find it odd that kotlin-jupyter notebooks still use the `.ipynb` file extension.

Maybe a little odd, but it did start with Python. I mean, Jupyter stands for Julia, Python, and R already - so it's not too weird they just keep using the same file format.

Re: Kotlin for data analysis

#50

Earlier quoted context omitted.

It's mostly ease of semantics -- in your example you use two layers of map and as a result need to do flatMap instead of just map twice In py, for list/set/dictionary/generator comprehensions, the format is always the same and always the same as if you were to do it as a normal nested loop, save for the statement being first instead of last (you can also do filters using normal if statement syntax, these go at the en…

Theres the zip function builtin, which I actually would have preferred oneToTen.zip(oneToTen2) { x, y -> x * y }

That's not actually equivalent. The initial version they included produces:

  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ...]
Whereas yours is just:

  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Post reply on HN