Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active November 24, 2020 02:46
Show Gist options
  • Save tateisu/3c5395e93c28e26ea1a074ccc37e0087 to your computer and use it in GitHub Desktop.
Save tateisu/3c5395e93c28e26ea1a074ccc37e0087 to your computer and use it in GitHub Desktop.
// parser with recursive call
suspend fun simpleParser(ch: Channel<Char>, nest: Int=0 ) {
while (true) {
when (val a = ch.receiveOrNull()) {
null, ']' -> break
'[' -> simpleParser(ch, nest + 1 )
else -> println("$nest $a")
}
}
}
// launch flow.collect() and send data to channel
suspend fun <T> Flow<T>.collectToChannel() = Channel<T>().apply {
coroutineScope{
launch {
[email protected] { send(it) }
close()
}
}
}
runBlocking {
simpleParser(
flowOf('a', '[', 'b', ']')
.collectToChannel ()
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment