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! šŸ“–