Skip to content

Instantly share code, notes, and snippets.

@mniami
Last active September 4, 2019 11:24
Show Gist options
  • Save mniami/ca76bd0a233e50e8f7be0fe9b0671449 to your computer and use it in GitHub Desktop.
Save mniami/ca76bd0a233e50e8f7be0fe9b0671449 to your computer and use it in GitHub Desktop.
Example of migration from java Optional to Kotlin
// That's pretty much an implementation of singleton btw
object Optional {
fun <T: Any> onNullable(obj: T?) : Iterable<T> {
if (obj != null) {
return listOf(obj)
}
return emptyList()
}
}
fun parseAndIncKotlin(number: String?): Int {
return Optional.onNullable(number)
.map {
Integer.parseInt(it)
}.map {
it + 1
}.ifEmpty {
listOf(0)
}.first()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment