Kotlin Koans

Kotlin Koans are a set of exercises that will help us to learn the Kotlin syntax. Each exercise is set up as a failing unit test, and it’s up to us to pass it. Working with Kotlin Koans can be done in one of two ways:

  1. We can play with Kotlin Koans over the internet.
  2. By installing the EduTools plugin and selecting the Kotlin Koans course, we may complete the tasks immediately inside IntelliJ IDEA or Android Studio.

Regardless of how we solve Koans, we can see the solution for each task by selecting “Show answer” in the online version or “Peek solution” in the EduTools plugin. We can examine the answer once we have completed the task to see how it compares to the one proposed. In the article, we will discuss the concept of Kotlin Koan by using it offline in the window application.

How to Use Kotlin Koan in IntelliJ Idea?

JetBrains has provided a plugin in IntelliJ idea which is called the EduTools plugin. We can install the plugin by going to the settings plugin section and searching for the plugin EduTools. This is a free edition that can be used by anybody in a community edition of IntelliJ idea. By installing the EduTools, we can get the Kotlin Koans.

Once you have installed the plugin, then go to file and there is a new option as “learn and teach”. From here, click on browse course. The browse course will take us to a list of courses that JetBrains provides to learn within the IntelliJ IDEA itself. Here, we are going to learn Kotlin Koan. So, choose the Kotlin Koans course shown in the below images.

The Kotlin Koan downloads dynamically all the courses which we can choose directly on the listed course and solves the particular problems. The following are the course list that Kotlin Koan provides:

Example # 1: Program of Kotlin Koan to Return String

The introduction part of the Kotlin Koan has several basic exercises problems which we need to solve one by one. Here, we have solved just one “Hello world” exercise for your convenience.

The function TODO() will raise an exception in the Kotlin Koans tasks. To finish Kotlin Koans, you must substitute the function call with appropriate code that solves the problem.

In the above task, we have to pass a string “Ok” in the start() function. We can see the details of the simple function on the right side of the code. Also, it will generate tests passed as “correct” shown at the end of the right corner.

Example # 2: Program of Kotlin Koan to Create Data Class

We have some tasks of Kotlin classes shown in the below image.

We have a task for the data class below. We just have to modify the class name. There is no need to modify the whole code.

To the generated class, apply the data modifier. For the class, the compiler will create a few useful approaches like equals/hashCode, function toString(), and others.

As we have declared a class with the correct implementation, it will generate a correct message as shown below. The peek solution will give you the solution of the task if you don’t know the correct answer.

Example # 3: Program of Kotlin Koan of Using Ranges Function

We have tasks of using the different conventions shown below.

Provide a function that validates if the date is within the range of the first and last dates using ranges (inclusive). We can create a range of comparable items. In Kotlin, checks are translated into contains calls and to range calls.

In the above task of range, we have just passed the first range to last in the “date” of the function “checkInRange”.

We have the correct answer to the above code, so it has generated “correct”.

Example # 4: Program of Kotlin Koan of Using Collections

Here, we have various Kotlin collections tasks and we have solved one for you.

It’s simple to combine Kotlin with Java code. Under the hood, Kotlin’s default collections are all Java collections. There are many extension functions in the Kotlin standard library that make working with collections easier. For example, operations that begin with the keyword ‘to’ and turn a collection into another: toSet or toList.

In the above task, we just have to include an extension function toSet() in the function.

The correct is shown in the check option. That means we have a correct input collection in the code.

Example # 5: Program of Kotlin Koan of Using Kotlin Properties

The properties section has the following task shown in the image:

To PropertyExample, add a custom setter. When the propertyWithCounter is assigned, the counter field is incremented.

Here in the code of the set() function block, we have a field as an object which we have initialized with the “v” and that “v” is called in the set() function. The counter is set with the post-increment operator which will increment every time we pass a value.

As a result, we received a valid result indicating that we passed the exam.

Example # 6: Program of Kotlin Koan of Using Function Literals with Receiver

In the builder’s section, we have the following listed tasks to solve:

We have used isEven and isOdd as extension functions by declaring them as values. Now, we have to fill the blanks in the code with appropriate declarations.

We have set the statement in the Boolean block of isEven and isOdd functions in the blanks as shown in the above implementation.

We have passed the task too as displayed in the image below.

Example # 7: Program of Kotlin Koan of Using Different Generic Functions

Now, the last is the Generic functions of the Kotlin Koan which we also have to pass.

We have implemented a partitionTo() function that separates a collection into two collections based on the predicate to make the code compile.

The standard library has a partition() function that always produces two newly formed lists. We have created a method that separates a collection into two separate collections, each with two arguments.

Here to go with our last task passed as shown in the image below.


The main goal of the article is to familiarize you with the new feature of Kotlin. The Kotlin playground as Kotlin Koan is for learning, practicing, and discussing Kotlin. It’s a challenge designed to help beginners learn Kotlin.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.