https://kotlinlang.org logo
#reaktive
Title
# reaktive
l

Luigi Scarminio

08/19/2020, 1:45 PM
Is there a proper way to filter an Observable stream and log the ones that have been left out?
a

Arkadii Ivanov

08/19/2020, 1:49 PM
Maybe
Copy code
observableOf(1, 2, 3, 4, 5)
            .filter { 
                (it % 2 == 0).also { 
                    if (!it) {
                        println(it)
                    }
                }
            }
Or define the following operator:
Copy code
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)
                }
            }
        }
🙂 1
l

Luigi Scarminio

08/19/2020, 1:55 PM
I liked the operator's solution. Thanks!
👍 1
2 Views