Notes on using Kotlin for competitive programming

Revision en17, by Spheniscine, 2019-11-04 06:34:04

I pretty much exclusively use Kotlin for competitive programming, mostly because it's the language I'm currently most comfortable with. Here are some scattered notes and tidbits about my experience which I think might be useful to others; if you have any tips/suggestions, feel free to let me know.

Primer

  • Kotlin has an official primer for competitive programming. However, the IO code suggested there is only so-so; it's definitely better than Scanner, but you definitely can save a lot of runtime in heavy input problems by using the classic Java combination of BufferedReader + StringTokenizer
My current IO template

Useful features

  • A lot less boilerplate than Java. Members are public by default. Type inference means a lot less "Pokémon speak". Variables and functions can be declared straight in the top-level of the file. (basically the equivalent of static functions). Fields have implicit getters and setters that can easily be overridden when necessary.

  • PHP-like string templates, e.g. "$id $cost"

  • Extension functions – syntactic sugar for static functions; gives more natural "afterthought" syntax, as well as allowing direct access to public members of the receiver

  • data classes – basically custom tuples. Allows convenient destructuring declarations too.

  • Has access to the data structures in the Java standard library (TreeMap, HashMap, PriorityQueue etc.), and also can use BigInteger and BigDecimal if needed

  • Functional idioms for collection manipulation – map, fold, filter, etc.

  • Sequences – lazy sequence generators, potentially infinite, has standard collection manipulation functions too. Using the sequence { ... } block function allows building a sequence using a scoped yield(value) function, reminiscent of Python

  • inline classes – allows the creation of a new type that wraps over a base type, but that is represented by an underlying type at runtime. Especially useful for "modulo $$$10^9 + 7$$$" problems, as I keep code for a ModInt class that overloads the arithmetic operators appropriately, but is represented as a plain int in JVM runtime. Keep in mind that they are experimental as of Kotlin 1.3, but that's fine for CP in my opinion

  • unsigned integer types in the standard library that use the inline class feature. Not used very often, but handy if needed

  • inline functions – tells the compiler to inline the function to call sites. Useful for higher-order functions (JVM won't need to create a new object for the lambda) as well as small functions that are called very often; basically, anything you might use a macro for in C++, you probably want to use an inline fun or inline val for

  • tailrec fun – tail recursion optimization

  • run block function – great way to write code that needs to shortcut (e.g. return@run "NO") without having to write a new function and pass every relevant argument

  • functions in functions – functions can be defined within e.g. the main function, so again, no having to pass lots of arguments or global variables. Keep in mind that these are represented as objects during runtime. It's too bad they can't be inline as of yet

Potential pitfalls

  • Generic wrappers for JVM primitive types can cause TLE for some problems. Use primitive arrays (IntArray etc.) whenever possible to avoid this, but see next point

  • Inherits the hack-prone quicksort from Java for primitive arrays. Easiest solution is to use generic arrays or lists instead, but due to the performance benefit of primitive arrays, I've took the trouble to write code that shuffles them randomly before sorting.

  • For Kotlin versions < 1.3.30, there is a bug that will throw an exception when using .asJavaRandom() on instances of kotlin.random.Random, including Kotlin's default instance. Either use Java's own Random class, or steal this wrapper:

class JavaRandom(val kt: Random) : java.util.Random(0) {
    override fun next(bits: Int): Int = kt.nextBits(bits)
    override fun setSeed(seed: Long) {}
}
Tags kotlin

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en17 English Spheniscine 2019-11-04 06:34:04 483 Tiny change: 'rapper: \n```\ncla' -> 'rapper: \n\n```\ncla'
en16 English Spheniscine 2019-11-04 05:59:47 3 Added new pitfall due to buggy asJavaRandom() function in STL (saved to drafts) (published)
en15 English Spheniscine 2019-11-04 05:59:09 949 Added new pitfall due to buggy asJavaRandom() function in STL (saved to drafts)
en14 English Spheniscine 2019-11-02 09:06:17 11 Tiny change: 'ke string declarations, e.g. `"' -> 'ke string templates, e.g. `"'
en13 English Spheniscine 2019-11-02 07:56:59 6 Tiny change: '\n- `run` function ' -> '\n- `run` block function '
en12 English Spheniscine 2019-11-02 07:03:11 1 Tiny change: 'suggested here is on' -> 'suggested there is on'
en11 English Spheniscine 2019-11-02 06:59:28 54
en10 English Spheniscine 2019-11-02 06:52:38 114
en9 English Spheniscine 2019-11-02 06:44:36 11 Tiny change: ' the hack-vulnerable quicksor' -> ' the hack-prone quicksor'
en8 English Spheniscine 2019-11-02 06:14:52 41
en7 English Spheniscine 2019-11-02 06:01:00 278 Tiny change: 're sorting\n' -> 're sorting.\n' (published)
en6 English Spheniscine 2019-11-02 05:50:09 1566 Tiny change: 'the `main`() function ' -> 'the `main` function '
en5 English Spheniscine 2019-11-02 05:34:38 911
en4 English Spheniscine 2019-11-02 05:17:49 455
en3 English Spheniscine 2019-11-02 05:12:20 203
en2 English Spheniscine 2019-11-02 05:08:48 584 Tiny change: 'shortcut (`return@ru' -> 'shortcut (e.g. `return@ru'
en1 English Spheniscine 2019-11-02 05:01:55 1106 Initial revision (saved to drafts)