Last active
August 29, 2015 14:11
-
-
Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Array<T>.component1() = this.get(0) | |
fun <T> Array<T>.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++ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment