venkat
04/06/2018, 1:17 AMproducers
(think: sources),
- actors
(=sinks) and
- Channels
(=edges),
Throw in:
- transforms
and
- pipes
for easy hookup
we end up with DAGs
, not unlike NodeJS’ streams.
Here’s an early proof of concept: https://github.com/venkatperi/kotlin-coroutines-lib/blob/master/src/main/kotlin/com/vperi/kotlinx/coroutines/experimental/TransformChannel.kt
runBlocking {
FS.createReader(inputFile.toPath())
.pipe(decodeUtf8())
.pipe(splitLines())
.pipe(spy({ println("> $it") }))
.pipe(contents({
assertEquals(lines.size, it.size)
}))
}
where:
// doesn't handle lines split across buffers
fun splitLines() = transform<String, String> {
var count = 0
input.consumeEach {
it.split("\n").forEach {
output.send("${count++}: $it")
}
}
}
More in the tests…