This file contains 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
#!/usr/bin/env bash | |
KOTLIN=$(find . -name "*.kt" -exec cat {} \; | wc -l) | |
JAVA=$(find . -name "*.java" -exec cat {} \; | wc -l) | |
echo Kotlin: $KOTLIN lines \($(echo scale=2\; 100 \* $KOTLIN / \( $KOTLIN + $JAVA \) | bc -l)%\) | |
echo Java : $JAVA lines \($(echo scale=2\; 100 \* $JAVA / \( $KOTLIN + $JAVA \) | bc -l)%\) | |
This file contains 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
// when is an expression, not a statement! | |
fun symbolOf(currency: Currency) : String { | |
return when(currency) { | |
Currency.USD -> "$" | |
Currency.EUR -> "€" | |
Currency.GBP -> "£" | |
Currency.BTC -> "₿" | |
Currency.ETH -> "⧫" | |
else -> { | |
log("warning: unknown symbol") |
This file contains 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
interface View { | |
fun click() | |
fun toggle() | |
} | |
class ViewImpl: View { | |
override fun click() { println("click!") } | |
override fun toggle() { println("toggle!") } | |
} |
This file contains 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
// until and step are infix functions | |
for(i in 0 until 100 step 2) println("$i") | |
// equivalent to: | |
for(i in 0.until(100).step(2)) println("$i") | |
// downTo is an infix function | |
for(i in 100 downTo 0) println("$i") | |
// shl, and, xor are infix functions | |
val i = (0x65acf9 shl 6) and 0x55 xor 0x80 |
This file contains 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
val list = listOf("Kotlin", "", "Java", "Groovy") | |
fun main() { | |
list.forEach { | |
if(it.isNullOrEmpty()) return@forEach // return with implicit label | |
println(it) | |
} | |
println("Done!") | |
} |
This file contains 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
// Kotlin's standard library extension | |
inline fun Array<String>.forEach(action: (String) -> Unit) { | |
for(str in this) { | |
action(str) | |
} | |
} | |
val list = listOf("Kotlin", "Java", "Groovy") | |
// a bare return statement in a lambda called from |
This file contains 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
inline fun greeter(action: () -> Unit) { | |
try { | |
println("Hello!") | |
action() | |
}finally{ | |
println("Goodbye!") | |
} | |
} | |
greeter { |
This file contains 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
enum class Direction { | |
NORTH, SOUTH, WEST, EAST | |
} | |
enum class Color(val rgb: Int) { | |
RED(0xFF0000), | |
GREEN(0x00FF00), | |
BLUE(0x0000FF) | |
} |
This file contains 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
// the returned value is the last expression of the try or catch block | |
val a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null } | |
fun printNumber(reader: BufferedReader) { | |
val number = try { | |
parseInt(reader.readLine()) | |
} catch (e: NumberFormatException) { | |
return | |
} |
This file contains 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
val listener = object: OnMenuClickListener, OnMenuExpandListener { | |
override fun onMenuClick(item: Menu) { ... } | |
override fun onMenuExpand(item: Menu) { ... } | |
} | |
menu.setOnClickListener(listener) | |
menu.setOnExpandListener(listener) |