Skip to content

Instantly share code, notes, and snippets.

@passsy

passsy/RxExt.kt

Created Jun 6, 2018
Embed
What would you like to do?
Allow Rx Single to map to nullable values by converting to Maybe. #kotlin
/**
* Maps the [Single] value [T] to a nullable type [R?] which will become [Maybe.empty] when `R == null`,
* otherwise [Maybe.just]
*/
inline fun <T, R> Single<T>.mapNullable(crossinline mapper: (T) -> R?): Maybe<R> = this.flatMapMaybe {
val result = mapper(it)
if (result == null) Maybe.empty() else Maybe.just(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment