Last active
August 29, 2015 14:11
-
-
Save ayato-p/c15d7487d3cfe9f82095 to your computer and use it in GitHub Desktop.
Revisions
-
ayato-p revised this gist
Dec 16, 2014 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ fun <T> Array<T>.component1() = this.get(0) fun <T> Array<T>.component2() = this.get(1) -
ayato-p revised this gist
Dec 16, 2014 . 1 changed file with 13 additions and 24 deletions.There are no files selected for viewing
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 charactersOriginal 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() { 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() } } } -
ayato-p created this gist
Dec 16, 2014 .There are no files selected for viewing
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 charactersOriginal 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++ }