kOptional
Extended Java8 Optional
Kotlin back-port for Android and JVM
Add kOptional to your build.gradle
compile "com.stepango.koptional:koptional:1.2.0"
Usage example:
arrayOf(null, "1", null, "2", null, "3")
.map {
it.toOptional()
}
.mapIndexed {
i, optional -> optional.ifEmpty {
Log.e("kOptional", "$i element is null, it's bad")
}
}
.map {
it.map {
"$it is not null "
}
}
.forEach {
it.ifPresent(::println)
}
// 1 is not null // 2 is not null // 3 is not null Log: // 0 element is null, it's bad // 2 element is null, it's bad // 4 element is null, it's bad