pakoito
04/10/2018, 10:25 AMgroostav
04/10/2018, 8:39 PMChannel
, where
val x = produce { send(3), send(4), send(5) }
val mapped = x.map { it * 2 }
would provide a channel containing 6
, 8
and 10
, while preserving the suspend (and close) semantics
meaning, I believe, its safely implemented with
inline fun <T, R> Channel<T>.map(
crossinline transform: (T) -> R
): Channel<R> = produce {
for(message in this){
val next = recieve()
//try-catch might be a big problem here.
send(transform(next))
}
send(transform(recieve()))
}
groostav
04/10/2018, 8:43 PMgroostav
04/10/2018, 8:43 PMflatMap
get a little confusing heregroostav
04/10/2018, 8:46 PMpakoito
04/10/2018, 8:48 PMpakoito
04/10/2018, 8:49 PMpakoito
04/10/2018, 8:49 PMpakoito
04/10/2018, 8:50 PMpakoito
04/10/2018, 8:50 PMpakoito
04/10/2018, 8:51 PMpakoito
04/10/2018, 8:51 PMpakoito
04/10/2018, 8:52 PMgroostav
04/11/2018, 6:14 AMproduce
Uncaught exceptions in this coroutine close the channel with this exception as a cause and the resulting channel becomes failed, so that any attempt to receive from such a channel throws exception.
groostav
04/11/2018, 6:14 AMChannel.map
kingsley
04/11/2018, 8:01 AM