# Katas in Kotlin

I’ve been meaning to get my hands on Kotlin for some time now and it’s certainly about time I got stuck in and learnt a new language. C# is my current go-to for backend programming, and it’s changing all the time. So much so that I’d say I learn something new about it almost weekly, meaning the thirst to learn a whole new language is somewhat quenched by the never-ending features.

## The learning method

Whenever it comes to learning a new programming language I adopt the jump in with two feet and hope I don’t break a leg strategy. I’ve documented the learning process before, so this blog will be much of the same just with a new language.

The essence of the strategy is to find a very simple programming problem. I use codewars where the easiest puzzles are called 8kyus. Now, an 8kyu is very simple but it’s not the problem that teaches us the language. It’s the steps taken to solve the problem in the entirely foreign ecosystem which help us build an understanding.

## The kata

Here’s the first 8kyu I found for Kotlin:

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.
Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren’t.

Sounds simple enough to me.

## Setting up

Let’s get started.
Codewars has its own built-in editor but I prefer to get knee-deep in the language using tools such as the debugger and setting up our own tests. This, in my opinion, is the best way to learn.

Let’s create a Kotlin project in Intellij:

Next I want to be able to execute code, let’s create a main method:

``````
fun main(arg: Array<String>) {
print("Hello world")
}
``````

That did the trick, now let’s solve the problem.

## Solving the Kata

Okay, so we need a function that has two arguments which are the number of petals that the pair have picked. Then we need to do a comparison to check the parity of each number to see whether they are in love.

I start off by creating the simplest solution with the aim to refactor:

``````fun loveFun(timmys: Int, sarah: Int) : Boolean {
if (timmys % 2 == 0 && sarah % 2 !== 0) {
return true
}
if (timmys % 2 != 0 && sarah % 2 == 0) {
return true
}
return false;
}``````

Next, I want to create my first test suite to ensure the function works and that any refactoring doesn’t result in a broken function.

Intellij does a lot of the heavy-lifting here: Right-click on the function | generate:

And it looks as though JUnit isn’t currently a dependency in my project, so I’ll have to add that:

``````class OppositesAttractTests {

@Test
fun areInLoveTests() {
assertTrue(areInLove(1, 4))
assertFalse(areInLove(2, 2))
assertTrue(areInLove(0, 1))
assertFalse(areInLove(0, 0))
}
}``````

Next let’s do a little refactoring.

``````fun areInLove(timmys: Int, sarah: Int) : Boolean{
return timmys % 2 !== sarah % 2;
}``````

And rerun the tests, all good, let’s submit this and take a look at the top solution:

``fun loveFun(flowerA: Int, flowerB: Int): Boolean = (flowerA + flowerB) % 2 == 1``

So if a Kotlin function contains just one expression we can omit the brackets and use an equals instead, like a variable assignment.

## Recap – What I’ve learned from doing this one problem

From just 1 kata I have learnt the following:

• How to set up a Kotlin project
• The different types of Kotlin projects you can create
• How the project structure looks
• How to add a library
• How to print to the console
• How to create a function
• The basic type system
• How to add a class
• How to add a test suite
• How to write an inline function

And many more subtle lessons and comparisons to other languages I’ve made.

## Repeat

Next, as explained in my previous blog on learning a new language I repeat the steps. Increasing the difficulty as I get comfortable.

Let’s tackle a 7Kyu.

A circular list is of finite size, but can infititely be asked for its previous and next elements. This is because it acts like it is joined at the ends and loops around. For example, imagine a CircularList of `[1, 2, 3, 4]`. Five invocations of `next()` in a row should return 1, 2, 3, 4 and then 1 again. At this point, five invocations of `prev()` in a row should return 4, 3, 2, 1 and then 4 again. Your CircularList is created by passing a vargargs parameter in, e.g. `new CircularList(1, 2, 3)`. Your list constructor/init code should throw an Exception if nothing is passed in.

And here’s the sample code they provide:

``````class CircularList<T>(vararg val elements: T) {
fun next(): T {
}

fun prev(): T {
}
}
``````

Already, just from looking at this sample code I have learnt a couple things:

• How to use generic types

Let’s have a crack at solving it:

``````class CircularList<T>(vararg val elements: T) {
var current = -1;

fun next(): T {
current = (current + 1) % elements.count();
return elements[current];
}

fun prev(): T {
current -= 1;
if (current < 0) current = elements.count() -1;
return elements[current];
}
}
``````

Let’s add the sample tests to our own testing file:

``````class Tests {
@Test
fun circularListTest() {
val xs = CircularList<String>("one", "two", "three")
assertThat(xs.next(), `is`("one"))
assertThat(xs.next(), `is`("two"))
assertThat(xs.next(), `is`("three"))
assertThat(xs.next(), `is`("one"))
assertThat(xs.prev(), `is`("three"))
assertThat(xs.prev(), `is`("two"))
assertThat(xs.prev(), `is`("one"))
assertThat(xs.prev(), `is`("three"))

val ys = CircularList<Int>(1, 2, 3, 4, 5)
assertThat(ys.prev(), `is`(5))
assertThat(ys.prev(), `is`(4))
assertThat(ys.next(), `is`(5))
assertThat(ys.next(), `is`(1))
assertThat(ys.next(), `is`(2))
assertThat(ys.next(), `is`(3))
assertThat(ys.next(), `is`(4))
assertThat(ys.prev(), `is`(3))
assertThat(ys.prev(), `is`(2))
assertThat(ys.next(), `is`(3))
assertThat(ys.next(), `is`(4))
assertThat(ys.next(), `is`(5))
assertThat(ys.next(), `is`(1))
assertThat(ys.next(), `is`(2))
assertThat(ys.next(), `is`(3))
}
}``````

The code I wrote passes all of those tests, but there’s something I’m missing. In the problem description, they explicitly specified that if no elements are passed to the constructor, then we throw an error. Let’s do that.

``````class CircularList<T>(vararg val elements: T) {
init {
if (elements.count() < 1) {
throw Exception();
}
}
var current = -1;

fun next(): T {
current = (current + 1) % elements.count();
return elements[current];
}

fun prev(): T {
current -= 1;
if (current < 0) current = elements.count() -1;
return elements[current];
}
}
``````

And that’s a success!

And there really isn’t much difference between mine and the top voted!

``````class CircularList<T>(vararg val elements: T) {
var i = -1
init {
if(elements.size == 0) throw Exception()
}
fun next(): T {
i = (i + 1) % elements.size
return elements[i]
}
fun prev(): T {
i = ( if(i < 1) elements.size else i ) - 1
return elements[i]
}
}``````

## What I’ve learned from the 7Kyu

• How to use generic types
• There isn’t any `new` keyword in Kotlin
• How to add and use constructor arguments
• How to use varargs
• How to throw exceptions
• How to create a body for the constructor so you can write logic
• How to create properties

## And the process continues

I’ll continue the process, but you get the idea. It takes me far longer to write what I’ve done than it does to do the problem. Although I will say, it’s helpful to acknowledge what you’ve learnt to really solidify your understanding. This blog is basically my notes, I hope you’ve enjoyed it!

In my next blog, I’ll be creating something a little more complicated using Kotlin, so that’ll be fun.