fun Array.component1() = this.get(0) fun Array.component2() = this.get(1) class HQ9Plus(private val src: String) { private var count = 0 fun eval() { src.forEach { when(it){ 'H' -> hello() 'Q' -> printSrc() '9' -> print99BottlesOfBeer() '+' -> increment() else -> null } } } private fun printSrc() = println(src) private fun hello() = println("Hello, world") private fun print99BottlesOfBeer() { 99.downTo(0).forEach { i -> var (before, after) = when (i) { 0 -> array("no more bottles", "99 bottles") 1 -> array("1 bottle", "no more bottles") 2 -> array("2 bottles", "1 bottle") else -> array("$i bottles", "${i - 1} bottles") } var action = if(i == 0){ "Go to the store and buy some more" } else { "Take one down and pass it around" } println("${before.capitalize()} of beer on the wall, $before of beer.") println("$action, $after of beer on the wall.") if(i != 0){ println() } } } private fun increment() = count++ }