Last active
September 4, 2019 11:24
-
-
Save mniami/ca76bd0a233e50e8f7be0fe9b0671449 to your computer and use it in GitHub Desktop.
Example of migration from java Optional to Kotlin
This file contains hidden or 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
// 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