Skip to content

Instantly share code, notes, and snippets.

@passsy
Created June 6, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passsy/777e98935d26f2070c54bcc389b63b3d to your computer and use it in GitHub Desktop.
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
/**
* 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