# Tips for golfing in Kotlin

Given Google's recent announcement of official Kotlin support for Android development, I thought it might be timely to poll the community for some awesome golfing tips for this relatively new JVM language.

Kotlin includes a unique combination of features among it's JVM siblings which makes it potentially attractive for golfing:

So, how do I squeeze the last few bytes out of my Kotlin program? One tip per answer, please.

• Would there be interest in a golfing language that shortens some of Kotlin's longer names, but doesn't add a lot of extras (at least initially)? I am thinking of making common 1 letter, making string char counts shorter and adding single letter strings with only 1 quote mark? – jrtapsell Sep 18 '17 at 18:03
• *Common functions – jrtapsell Sep 18 '17 at 18:15
• Seems Kotlin golfing interest is not that high :( data.stackexchange.com/codegolf/query/793250/top-kotlin-golfers – jrtapsell Jan 28 '18 at 22:58
• I plan to start submitting more Kotlin solutions! I'll have to check out that project of yours as well. – Tyler MacDonell Jan 30 '18 at 0:48

# Extension Functions

Extension functions can really help to reduce the names of built in methods, and chains of them, an example could be:

fun String.c() = this.split("").groupingBy{it}.eachCount()

but this only helps if:

A) The call is long enough to cancel out the definition

B) The call is repeated

# Use of lambdas rather than methods

Lambdas can return without using the return keyword, saving bytes

# KotlinGolfer

A project I have started here which takes pretty Kotlin code and gives posts with tests and TIO links automatically

# Use + instead of toString

As one might expect, String overloads the + operator for string concatenation, like so.

print("Hel" + "lo")


However, checking the docs tells us that it accepts Any?, not just String. As stated:

Returns a string obtained by concatenating this string with the string representation of the given other object.

In other words, String + anything makes sure to call .toString() on the right hand side before concatenating. This allows us to shorten it.toString() to ""+it, a massive 8 byte savings at best and 6 bytes at worst.

# Use fold instead of joinToString

Related to the above, if you're calling map and then joinToString, you can shorten that by using fold instead.

list.map{it.repeat(3)}.joinToString("")
list.fold(""){a,v->a+v.repeat(3)}

• TIL fold is a thing, nice – Quinn May 13 at 18:29

Starting from 1.3 you can omit args in fun main() completely, thus shaving off 18 chars (that is, the length of args:Array<String>).

# Defining Int's in params

This will likely have some pretty specific use cases where it may be worth it, but in recent question I golfed I found I could save a few bytes by defining my variable as optional parameters rather than defining them in the function.

Example from my answer to this question:

defining variable in the function:

fun String.j()={var b=count{'-'==it}/2;var a=count{'/'==it};listOf(count{'o'==it}-a,a-b,b)}

defining variables as params:

fun String.j(b:Int=count{'-'==it}/2,a:Int=count{'/'==it})=listOf(count{'o'==it}-a,a-b,b)

because var a= is the same length as a:Int= it will be the same number of bytes to define them (this is only the case for Int) however since I now only have 1 line in the function I can drop the {} and I also drop a single ; (the other is replaced with a ,)

So if there are any functions that require defining an Int, and would be a 1 liner if you didn't define the int in the function - then doing it as a parameter will save a couple bytes