Created
November 8, 2019 08:40
-
-
Save vitusortner/96215479f6afbc747e85bcc5113299a4 to your computer and use it in GitHub Desktop.
RxJava mapNotNull
This file contains hidden or 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
/** | |
* 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