if you tell me which API is necessary to wrap, I c...
# coroutines
p
if you tell me which API is necessary to wrap, I could take a look for the next version
g
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
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
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