Skip to content

Instantly share code, notes, and snippets.

@lucidfrontier45
Created January 28, 2016 15:26
Show Gist options
  • Select an option

  • Save lucidfrontier45/8d3dddc0d8a0d232e997 to your computer and use it in GitHub Desktop.

Select an option

Save lucidfrontier45/8d3dddc0d8a0d232e997 to your computer and use it in GitHub Desktop.
make kotlin's nullables act like scala's option
package com.frontier45
/**
* Created by du on 16/01/29.
*/
fun <T1, T2> T1?.map(f: (T1) -> T2): T2? {
return if (this != null) {
f(this)
} else {
null
}
}
fun <T> T?.forEach(f: (T) -> Unit): Unit {
if (this != null) {
f(this)
}
}
fun <T> T?.getOrElse(f: () -> T): T {
return this ?: f()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment