https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

pakoito

04/10/2018, 10:25 AM
if you tell me which API is necessary to wrap, I could take a look for the next version
g

groostav

04/10/2018, 8:39 PM
I believe the one both me and @kingsley were thinking of is
Channel
, where
Copy code
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
Copy code
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())) 
}
and actually, maybe this is why we wouldnt want to use channels here, because they have no error mechanism for currying exceptions
further, the functional programmers favourite
flatMap
get a little confusing here
Actually I think this is a fascinating point because I think its raising the question of whether we can express suspension as a monad
p

pakoito

04/10/2018, 8:48 PM
we can express any monad as a comprehension
as long as the semantics remain on the same thread
the moment you jump thread you get a divide between datatypes that support asynchrony and those who don't
I'm exploring that at the moment
job may have a monad, we had it at some point IIRC
dunno about channels tho
g

groostav

04/11/2018, 6:14 AM
also, regarding exceptions, Our illustrious coroutine commander-in-cheif has outsmarted me. This is from the docs of
produce
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.
meaning you dont need special try-catch semantics on
Channel.map
2 Views