Created
January 12, 2020 21:27
-
-
Save alexrios/a75150b83ab5dcca63be49bb6bd50c10 to your computer and use it in GitHub Desktop.
Using pipeline pattern with Kotlin channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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