Learning Kotlin

Introduction
Hello everyone! š
The year of 2023 has come closely to an end, and I want to write a short article about my experience with Kotlin and what other programming languages Iāve tried this year.
Outside work, I like experimenting with new programming languages and technologies. I donāt usually build side projects in the languages and frameworks that I use full time at work because I donāt find it fun. I like to learn new things.
So I have started experimenting with Dart, Rust, Swift and Kotlin, and I came to the conclusion that my preferred choice is Kotlin.
In my opinionā¦
Dart is a nice and language and has great tooling, but I donāt see it used anywhere beyond Flutter. So I donāt have a lot of use cases for it.
Rust is a great language with great tooling. But I donāt like fighting with the borrow checker and macros. š I donāt see myself as a systems programmer, so I donāt have a lot of use cases for Rust.
Swift is a great language, and the tooling Iāve used is not great, I donāt like XCode at all.
I also donāt have an Apple computer that I use personally all the time. It feels to me that Apple kind of neglected the language as well. Even though itās open-source and came out in 2015 when I was a first-year student in college, I donāt see it used anywhere outside Appleās ecosystem. Which is a shame because itās really an interesting language.
Kotlin, the language of the future
So far Iāve used Kotlin for writing a small Android Application called Image Tagger itās currently in review by Google, and for solving the Advent of Code 2023 puzzles.
What I like about Kotlin is that itās a modern language built on the JVM, itās easy to learn, and itās fun to write code in it āØ.
Unfortunately, the language is not as popular yet. I hope that in the future more people will start using it.
It allows you to target multiple platforms such as Android, iOS, Web, Desktop, and even embedded devices. Imagine
having the expressiveness of Kotlin on your Raspberry Pi. 𤯠or building cat š in Kotlin using functional programming
features and running it as a native application on your Linux machine. š§
Advent of Code 2023 - Day 6
To show you some Kotlin code, Iāll share with you my solution for the Advent of Code 2023 Day 6 puzzle. The puzzle is about a boat race, an input is given, and you need to read the input, parse it then write the solution for the puzzle.
The input looks like this:
Time: 7 15 30
Distance: 9 40 200
To read the input Iāve created a data class Race that represents a boat race then parsed it using Kotlinās
functional programming features and lambdas.
data class Race(val raceTime: Long, val distance: Long)
class WaitForIt : Puzzle("2023", "6") {
private fun getRaceTimesPart1(): List<Race> = inputData.map {
// Split on spaces, ignore first item, transform tokens to integers
it.split(Regex("\\s")).drop(1).map { it.trim().toLongOrNull() }.filterNotNull()
}.zipWithNext().map {
// Take the zipped lines (since we only have 2) and transform them into races.
it.first.mapIndexed { index, i -> Race(i, it.second[index]) }
}.flatten().also {
println("The input is $it")
}
/// ...
}
Now that I have a List<Race> I can execute my logic for solving the first part of the puzzle:
I generate all the possible race combinations, and then I count the number of ways the record was beaten.
/// ...
override fun partOne() {
getRaceTimesPart1().map {
// here we transform a race into a list of all the possible combinations that we can have
(1..<it.raceTime).count { time -> (it.raceTime - time) * time > it.distance }
}.reduce { acc, i -> acc * i }.also {
println("The number of ways the record was beaten $it.")
}
}
/// ...
The second part of the puzzle is similar to the first one, but the input is different.
Conclusion
Kotlin is a great language, and Iām looking forward to using it more in 2024. Hopefully Iāll use Ktor to write some of my backend code for other applications Iāll build.
I hope youāve enjoyed this article! š