Created
June 6, 2018 16:01
-
-
Save passsy/777e98935d26f2070c54bcc389b63b3d to your computer and use it in GitHub Desktop.
Allow Rx Single to map to nullable values by converting to Maybe. #kotlin
This file contains 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
/** | |
* 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