ReactiveX / RxKotlin

RxJava bindings for Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add mapNotNull extension

derektom14 opened this issue · comments

Add an extension method to each Rx chain that has a value to match Kotlin's mapNotNull.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-not-null.html

Sample implementations:

fun <T, R> Single<T>.mapNotNull(transform: (T) -> R?) = flatMapMaybe { value -> 
    Maybe.fromCallable {
        transform(value)
    }
}

fun <T, R> Observable<T>.mapNotNull(transform: (T) -> R?) = flatMapIterable { value -> 
    listOfNotNull(transform(value))
}

I have created PR #251. Hope maintainer will look at it