NercNews's blog

By NercNews, history, 7 years ago, In English

text

Let us introduce the new programming language in ICPC: Kotlin. It is modern and developing language created by our sponsor JetBrains. Kotlin is inspired by Java and as Java is named after the island. Currently, Kotlin programs are compiled into JVM bytecode, all Java written code can be used from Kotlin sources and Kotlin written code can be used from Java sources as well out of the box. Kotlin being developed now most of the standard libraries are Java library classes, making Kotlin a programing language that is already used in many projects being the main language of their development.

Comparing to Java language some Java disadvantages fixed and new features added. Some of them we will see in today's solution of ICPC World Finals 2016 problem C (101242C - Ceiling Function). Less boilerplate code and syntactic sugar added

  1. new operator omitted
  2. data classes implement hashCode, equals and toString methods depending on constructor parameters
  3. operator overloading
  4. with function, to implement a block of code, making captured value as this
  5. when operator providing better readability of conditional operator in some cases
  6. type inference (IDE supports hints on variable and function types, it's just you don't have to type it)
  7. functional programming style
import java.util.*

data class Tree(var left: Tree? = null, var right: Tree? = null) {
    var value = 0
}

operator fun Tree?.plus(x: Int): Tree? {
    if (this == null) return Tree().apply { value = x }
    if (x < value) {
        left += x
    } else {
        right += x
    }
    return this
}

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val n = nextInt()
    val k = nextInt()
    val a = Array(n) { IntArray(k) { nextInt() } }
    println(a.map { it.fold(null, Tree?::plus) }.toSet().size)
}

You can try Kotlin at: https://try.kotlinlang.org

  • Vote: I like it
  • +107
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +54 Vote: I do not like it

Just wondering, did somebody use Kotlin during last NEERC?

»
7 years ago, # |
  Vote: I like it +38 Vote: I do not like it

I tried Kotlin several years ago on the contest Kotlin Challenge. It took me about 5 minutes to declare 5-dimensional array. The resulting code looked awful. Can somebody share an example of modern declaration equivalent to int[][][][][] states = new int[5][6][7][8][9];?

  • »
    »
    7 years ago, # ^ |
    Rev. 4   Vote: I like it +26 Vote: I do not like it

    From the last example I generated


    fun main(args: Array<String>) { val states = Array(5) {Array(6){Array(7){Array(8){Array(9){0}}}}}; println(states[1][2][3][4][5]); }

    which seems to work.

    Not so short either but at least it's not quadratic in number of dimensions as code I used when participated in Kotlin Challenge

    UPD: last dimension should probably be IntArray to avoid boxing

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's very nice to see "Kotlin" in "ICPC" , but no one in my region will care , as it is not allowed to use in .

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    yeah, me neither. In my region, they only accept C++, Pascal, Java and maybe Python.