Skip to content

Instantly share code, notes, and snippets.

@sanity
Created June 5, 2018 17:01
Show Gist options
  • Select an option

  • Save sanity/75d7b28b1e7690879020285631435d99 to your computer and use it in GitHub Desktop.

Select an option

Save sanity/75d7b28b1e7690879020285631435d99 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.*
fun main(args: Array<String>) = runBlocking {
val producer = produce {
for (v in 0..100) {
send(v)
delay(100)
}
close()
}
fun doubler(producer : ReceiveChannel<Int>, consumer : SendChannel<String>) = launch {
producer.consumeEach { v ->
consumer.send((v * 2).toString())
delay(500)
}
}
fun consumer(producer : ReceiveChannel<String>) = launch {
producer.consumeEach { v ->
println(v)
}
}
val chan = Channel<String>()
doubler(producer, chan)
consumer(chan).joinChildren()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment