diff --git a/src/main/kotlin/org/wycliffeassociates/otter/common/utils/RxUtils.kt b/src/main/kotlin/org/wycliffeassociates/otter/common/utils/RxUtils.kt new file mode 100644 index 0000000..7d9a6c5 --- /dev/null +++ b/src/main/kotlin/org/wycliffeassociates/otter/common/utils/RxUtils.kt @@ -0,0 +1,14 @@ +package org.wycliffeassociates.otter.common.utils + +import io.reactivex.Observable + +/** + * Apply the given function as with `map()`, but remove any resulting nulls from the + * observable stream. + * + * This is how `observable.map(f).filter { it != null }` should behave, but it keeps + * nulls from ever appearing in an Observable, which would cause a crash in that + * two-step map/filter example. + */ +fun Observable.mapNotNull(f: (T) -> R?): Observable = + concatMapIterable { listOfNotNull(f(it)) } \ No newline at end of file