Skip to content

Instantly share code, notes, and snippets.

@bwolf
Last active July 31, 2019 18:17
Show Gist options
  • Save bwolf/501717f50d696e91249b965e6544ee86 to your computer and use it in GitHub Desktop.
Save bwolf/501717f50d696e91249b965e6544ee86 to your computer and use it in GitHub Desktop.
Kotlin Result flatMap
inline fun <R, T> Result<T>.flatMap(transform: (value: T) -> Result<R>): Result<R> {
return when {
isSuccess -> {
assert(this.getOrNull() != null)
transform(this.getOrNull()!!)
}
else -> {
assert(this.exceptionOrNull() != null)
Result.failure(this.exceptionOrNull()!!)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment