Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.
Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.
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