Skip to content

Instantly share code, notes, and snippets.

View renaudcerrato's full-sized avatar

Renaud Cerrato renaudcerrato

View GitHub Profile
@renaudcerrato
renaudcerrato / kotlin-stats.sh
Last active August 26, 2019 08:56
Kotlin vs Java Statistics
#!/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)%\)
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 16, 2019 06:49
when example
// 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")
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 15, 2019 20:20
Kotlin InterfaceDelegation
interface View {
fun click()
fun toggle()
}
class ViewImpl: View {
override fun click() { println("click!") }
override fun toggle() { println("toggle!") }
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 15, 2019 18:50
Kotlin Infix Functions (Standard Library)
// 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
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 12, 2019 12:20
Kotlin Return with Labels
val list = listOf("Kotlin", "", "Java", "Groovy")
fun main() {
list.forEach {
if(it.isNullOrEmpty()) return@forEach // return with implicit label
println(it)
}
println("Done!")
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 12, 2019 12:20
Kotlin Local Returns
// 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
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 9, 2019 06:52
Kotlin Inline Function
inline fun greeter(action: () -> Unit) {
try {
println("Hello!")
action()
}finally{
println("Goodbye!")
}
}
greeter {
@renaudcerrato
renaudcerrato / snippet.kt
Last active March 9, 2019 06:20
Kotlin Enum
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
@renaudcerrato
renaudcerrato / snippet.kt
Created February 24, 2019 08:56
Kotlin Try/Catch expression
// 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
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active February 23, 2019 06:52
Kotlin Anonymous Objects
val listener = object: OnMenuClickListener, OnMenuExpandListener {
override fun onMenuClick(item: Menu) { ... }
override fun onMenuExpand(item: Menu) { ... }
}
menu.setOnClickListener(listener)
menu.setOnExpandListener(listener)