172 points · by saikatsg · 2y ago · 104 comments
And it's not necessarily the most efficient path either. 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.
For data engineering, Kotlin has a lot to offer. I wouldn't necessarily recommend it because it's all kind of niche. But it kind of works as well. If you aren't afraid of tinkering with it, there are a lot of other niche solutions out there as well that aren't python.
Kotlin is what I reach for when I want to get stuff done in a hurry. Part of that is just my limitation. It's what I know and I kind of grew up on JVM languages. I'm well aware that's not necessarily optimal and that that's just a bias I have. But objectively, it has a lot of nice things over modern python as well.
Kotlin is a modern language, it's a lot more expressive than python. It has a a great library ecosystem. Including some stuff that does not depend on the JVM. And even though it's kind of niche for a lot of things I use it for (e.g. developing reactive web frontends), it holds up well and rarely disappoints me.
People use python because everybody else uses python. That's it. It's not particularly good at anything it does. But it will get the job done and I can do it. But that just isn't good enough for me. It's the visual basic for data science. And that's not a compliment. Being idiot proof is it's main feature. But that doesn't make it the smart choice.
I used to love python, but I feel like the attempts to tack on typing have really undermined it.
The “freedom” of being untyped is nice for quick little scripting things, but being typed is an absolute godsend in a real codebase.
Instead of making python pseudo-typed with messy annotations, the scientific/research community should have built out tools in an actually typed language. I think Julia hung around for years with the expectation that people were going to do that
Even if you look only at the JVM: Scala is much older.
What makes Kotlin more expressive? I understand it has some functional features but I've never seen anything dramatically flexible.
Python, meanwhile, has always felt pretty awkward to me when it comes to data transformations. Comprehensions are okay, but they feel like they are special casing what should be a bunch of standardized helper functions operating on lambdas, as a sort of ugly workaround to the fact that Python refuses to implement proper lambdas. And when you can't use a comprehension, you're stuck with a pretty awkward collection of helper methods that are hard to find and use correctly and which are severely handicapped in expressivity by the lack of a proper lambda.
[1] https://www.reddit.com/r/Kotlin/comments/mh2z5u/comment/gt2n...
Scope functions are also great.
And the syntactic sugar where `foo({ a -> a })` and `foo { a -> b }` are the same makes code so much more readable.
I've done Python for a project at a previous job for a few months and it made me realize just how awful Python is, especially because you can't chain functions on collections as easily as you can in Kotlin. I also made me realize that I don't like dynamically typed languages.
Scala has it beaten by a long shot, in my opinion, and is hands down the best of any language around collections.
Python has lambdas. What do you mean by "proper" lambdas?
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 functionsTheres 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
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? [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]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)
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()
)I used to write a lot of Python, I now write a lot of Rust, and the Rust iterator chains feel inordinately more powerful, and list comprehensions feel semantically backwards to me now: what you’re doing, what you’re doing it to, and whether to do it conditionally are all out of order.
To me, Python feels “expressive” because you can “do stuff to make it work” not because of any inherent design that lets you properly express what you’re trying to do.
Personally, I see Kotlin as the closest thing to a statically typed Smalltalk that we have among major languages, and that's a major draw.
A key part here is that Kotlin closures are fully-featured equivalents of Smalltalk blocks (up to and including even non-local returns [1]), whereas in many other languages that falls short. Java does not allow mutation of local variables and Python restricts lambdas to normal expressions.
I find code whose behavior can be parameterized by code to be an essential feature of modern-day programming and this should be as frictionless as possible.
This is also a situation where syntax matters, and while it isn't quite as nice as Smalltalk, Kotlin's syntax (esp. with trailing closures) make such code as readable as possible in a brace-style language with minimal additional syntactic noise.
In a similar vein, the functionality of Smalltalk's cascades is offered through scope functions [2], especially `.run {}`.
But ultimately, fully-featured closures (and the fact that they are widely used in the standard library) power a lot of the things that people seem to like about Kotlin.
That does not mean that there aren't downsides. The limitations of running on the JVM are one (e.g. while Kotlin has workarounds for the JVM's type erasure, they're still workarounds), and then Gradle is arguably Kotlin's weakest point (which apparently even JetBrains are seeing, given their investment in Amper).
That said, personally I'd say Kotlin's static typing and performance would be the primary reasons for me to reach for Kotlin over Python, not necessarily expressiveness. Type annotations in Python + mypy etc. just aren't the same experience, and writing performance-sensitive code in Python can be very tricky/hacky when you can't delegate the hot paths to numpy or other existing C/C++/Rust libraries.
Conversely, Python often has a leg up when it comes to fast prototyping and scripting, even with Kotlin Worksheets in IntelliJ IDEA and with kscript.
[1] Which, to be clear, are a nice-to-have thing, not essential, but still impressive that even that was covered, when previously Ruby was the only major language I know of that did it.
Maybe if Kotlin Multiplatform really takes off.
[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 iterators.
But I know that I’m in the minority that likes SQL
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.Thank you for providing an example because I was a little confused by a couple other comments mentioning yield...
Sounds like it has a good Python interop story.
https://i.imgur.com/SjbtX2z.png
https://www.andrewheiss.com/blog/2024/07/08/fun-with-positro...
Part of the appeal of Kotlin is Intellij offering an enormous amount of smart features that other IDEs simply don't have at all for any language.
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.
> 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.
Your criticisms of OP's argument are more disingenuous than OP: no one said Python was a fad or that the ecosystem wasn't useful. See here:
> 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 ecosystem is great.
They're just saying that Python-the-language isn't their favorite (largely subjective but with a lot of agreement from much of the community) and they mention offhand that Python's package management is a disaster (which is 100% accurate).
For example > Package management is a disaster > The interpreter is slow > The syntax is not expressive
Is your particular experience with pip bad? Or is it poetry/conda or another upstart project? Are you working with a monorepo? In what use case is the interpreter slow? The syntax is not expressive (for what)?
My point about tooling/utility is that how you use a tool matters a lot (and maybe I didn't express that in my original reply).
I'll leave this here as well, I think it sufficiently implies the notion of Python being a fad and not particularly useful.
Also, practically it's only really viable to use IntelliJ as editor, since the language server in e.g. VS Code is pretty bad.
Kotlin without its main platforms (JVM and Android) and the ecosystem of Java libraries doesn't stand a chance against the current landscape of modern hybrid languages. Unless something major happens on the WebAssembly front maybe, but even there I don't see how Kotlin would be particularly well positioned.
> For other Jupyter clients, you'll need to install the Kotlin Kernel separately using conda, pip, or sources.
I think `pip install kotlin-jupyter-kernel` is all you need to add Kotlin to a standard Jupyter environment. I don't know if editors can deal with this stuff, though.