Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.

Select an option

Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.

Revisions

  1. ayato-p revised this gist Dec 16, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion HQ9Plus.kt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    package kt

    fun <T> Array<T>.component1() = this.get(0)
    fun <T> Array<T>.component2() = this.get(1)
  2. ayato-p revised this gist Dec 16, 2014. 1 changed file with 13 additions and 24 deletions.
    37 changes: 13 additions & 24 deletions HQ9Plus.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    package kt

    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

    @@ -18,38 +23,22 @@ class HQ9Plus(private val src: String) {
    private fun hello() = println("Hello, world")

    private fun print99BottlesOfBeer() {
    var before: String
    var after: String
    var action: String

    99.downTo(0).forEach { i ->
    when(i) {
    0 -> {
    before = "no more bottles"
    after = "99 bottles"
    }
    1 -> {
    before = "1 bottle"
    after = "no more bottles"
    }
    2 -> {
    before = "2 bottles"
    after = "1 bottle"
    }
    else -> {
    before = "$i bottles"
    after = "${i-1} bottles"
    }
    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")
    }

    action = if(i == 0){
    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.
    $action, $after of beer on the wall.""")
    println("${before.capitalize()} of beer on the wall, $before of beer.")
    println("$action, $after of beer on the wall.")
    if(i != 0){ println() }
    }
    }
  3. ayato-p created this gist Dec 16, 2014.
    58 changes: 58 additions & 0 deletions HQ9Plus.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    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() {
    var before: String
    var after: String
    var action: String

    99.downTo(0).forEach { i ->
    when(i) {
    0 -> {
    before = "no more bottles"
    after = "99 bottles"
    }
    1 -> {
    before = "1 bottle"
    after = "no more bottles"
    }
    2 -> {
    before = "2 bottles"
    after = "1 bottle"
    }
    else -> {
    before = "$i bottles"
    after = "${i-1} bottles"
    }
    }

    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.
    $action, $after of beer on the wall.""")
    if(i != 0){ println() }
    }
    }

    private fun increment() = count++
    }