Thread
#reaktive
    l

    Luigi Scarminio

    2 years ago
    Is there a proper way to filter an Observable stream and log the ones that have been left out?
    Arkadii Ivanov

    Arkadii Ivanov

    2 years ago
    Maybe
    observableOf(1, 2, 3, 4, 5)
                .filter { 
                    (it % 2 == 0).also { 
                        if (!it) {
                            println(it)
                        }
                    }
                }
    Or define the following operator:
    inline fun <T> Observable<T>.filter(crossinline predicate: (T) -> Boolean, crossinline onFiltered: (T) -> Unit): Observable<T> =
            filter { item ->
                predicate(item).also { 
                    if (!it) {
                        onFiltered(item)
                    }
                }
            }
    l

    Luigi Scarminio

    2 years ago
    I liked the operator's solution. Thanks!