Skip to content

Instantly share code, notes, and snippets.

@alexrios
Created January 12, 2020 21:27
Show Gist options
  • Save alexrios/a75150b83ab5dcca63be49bb6bd50c10 to your computer and use it in GitHub Desktop.
Save alexrios/a75150b83ab5dcca63be49bb6bd50c10 to your computer and use it in GitHub Desktop.
Using pipeline pattern with Kotlin channels
package app
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
sq(gen(listOf(1, 2, 3, 4, 5))).consumeEach { println(it) }
}
private fun CoroutineScope.gen(nums: List<Int>) = produce {
nums.forEach { send(it) }
}
private fun CoroutineScope.sq(input: ReceiveChannel<Int>) = produce {
input.consumeEach { send(it * it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment