Last active
November 24, 2020 02:46
-
-
Save tateisu/3c5395e93c28e26ea1a074ccc37e0087 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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