https://kotlinlang.org logo
Title
f

feroz_baig

07/25/2017, 11:14 AM
Anyone here who worked with coroutines, please share your opinions. If familiar with RxJava2, please contrast both of them as well
r

rafal

07/25/2017, 11:19 AM
Creating custom operator is waaaaay easier with coroutines. So creating e.g. rx-like operator is a breeze
fun <T, R> ReceiveChannel<T>.flatMap(context: CoroutineContext = Unconfined, onError: (Throwable) -> Unit = logOnError, mapper: suspend (T) -> ReceiveChannel<R>): ReceiveChannel<R> = produce<R>(context) {
    try {
        for (elem in this@flatMap)
            for (mapped in mapper(elem))
                send(mapped)
    } catch (t: Throwable) {
        onError(t)
    }
}
that's all it took for me to create
flatMap
"operator"
👍 1
Also ask for other opinions on #coroutines as my opinion might be somehow biased