Skip to content

Instantly share code, notes, and snippets.

Cerrato Renaud renaudcerrato

Block or report user

Report or block renaudcerrato

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am renaudcerrato on github.
  • I am renaudcerrato (https://keybase.io/renaudcerrato) on keybase.
  • I have a public key ASCQKvRETARxko5cZvuoIqdmRaYbsSm-KO10Oix554lilQo

To claim this, I am signing this object:

@renaudcerrato
renaudcerrato / snippet.kt
Last active Mar 12, 2019
Kotlin Return with Labels
View snippet.kt
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 Mar 12, 2019
Kotlin Local Returns
View snippet.kt
// 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 Mar 9, 2019
Kotlin Inline Function
View snippet.kt
inline fun greeter(action: () -> Unit) {
try {
println("Hello!")
action()
}finally{
println("Goodbye!")
}
}
greeter {
@renaudcerrato
renaudcerrato / snippet.kt
Created Feb 24, 2019
Kotlin Try/Catch expression
View snippet.kt
// 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
}
View snippet.kt
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
@renaudcerrato
renaudcerrato / snippet.kt
Last active Mar 15, 2019
Kotlin Infix Functions (Standard Library)
View snippet.kt
// 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 Feb 23, 2019
Kotlin Infix Functions
View snippet.kt
class Price(val value: Double, val currency: Currency)
infix fun Int.euro(cents: Int): Price {
return Price(toDouble() + cents / 100.0, Currency.EURO)
}
val price = 1 euro 42
// equivalent to:
val price = 1.euro(42)
@renaudcerrato
renaudcerrato / snippet.kt
Last active Feb 23, 2019
Kotlin Anonymous Objects
View snippet.kt
val listener = object: OnMenuClickListener, OnMenuExpandListener {
override fun onMenuClick(item: Menu) { ... }
override fun onMenuExpand(item: Menu) { ... }
}
menu.setOnClickListener(listener)
menu.setOnExpandListener(listener)
@renaudcerrato
renaudcerrato / snippet.kt
Created Feb 22, 2019
Kotlin Companion Object
View snippet.kt
class Person private constructor(val name: String, val age: Int) {
companion object Factory {
fun create(name: String, age: Int) = Person(name, age)
}
}
// members of the companion object can be called using the class name as qualifier
val bob = Person.create("Bob", 21)
You can’t perform that action at this time.