flatmap
inline fun <T> T.flatmap(predicate: T.() -> Boolean, falsy: T.() -> T = ::caller, truthy: T.() -> T = ::caller): T
Syntax-sugar for a lambda that folds a receiver type T into something else of the same type.
This should primarily be used when chaining.
val result: String = "Hello"
// Before:
val a = 7
val b = if (a % 2 == 0) a else a * 2
val c = b.toString()
// After:
7.flatmap(predicate = { this % 2 == 0 }) { this * 2 }
.toString()
Content copied to clipboard
Parameters
T
The type of the receiver.
predicate
The predicate to evaluate on the receiver.
falsy
The lambda to call if the predicate returns false
.
truthy
The lambda to call if the predicate returns true
.