Skip to content

Instantly share code, notes, and snippets.

@vitusortner
Created November 8, 2019 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitusortner/96215479f6afbc747e85bcc5113299a4 to your computer and use it in GitHub Desktop.
Save vitusortner/96215479f6afbc747e85bcc5113299a4 to your computer and use it in GitHub Desktop.
RxJava mapNotNull
/**
* Applies [transform] to values from the [Observable] and forwards values with non-null results.
*/
inline fun <T : Any, S : Any> Observable<T>.mapNotNull(
crossinline transform: (T) -> S?
): Observable<S> {
return this
.flatMap {
val result = transform(it)
if (result == null) {
Observable.empty()
} else {
Observable.just(result)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment