-
-
Save Takhion/49e21493e9be13627fc3383499abaf60 to your computer and use it in GitHub Desktop.
Port Kotlin's awesome filterIsInstance() to RxJava — one call to filter by type and map-cast to that type
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
package me.seebrock3r.extensions.reactivex | |
import io.reactivex.Flowable | |
import io.reactivex.Maybe | |
import io.reactivex.Observable | |
import io.reactivex.Single | |
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe | |
inline fun <reified R> Flowable<*>.filterIsInstance(): Flowable<R> = | |
filter { it is R } | |
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe | |
inline fun <reified R> Observable<*>.filterIsInstance(): Observable<R> = | |
filter { it is R } | |
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe | |
inline fun <reified R> Single<*>.filterIsInstance(): Maybe<R> = | |
filter { it is R } | |
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe | |
inline fun <reified R> Maybe<*>.filterIsInstance(): Maybe<R> = | |
filter { it is R } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment