Created
March 27, 2020 10:23
-
-
Save nomisRev/09532c58e222379ca17501b246a96d1d to your computer and use it in GitHub Desktop.
Process chunks with IO - Question on KotlinLang Slack
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
import arrow.fx.IO | |
import arrow.fx.extensions.fx | |
fun chunks(): List<List<String>> = listOf( | |
listOf("a", "b", "c"), | |
listOf("d", "e", "g") | |
) | |
fun process(prevChunks: List<String>, rest: List<List<String>>, merged: List<String>, size: Int, max: Int): IO<List<String>> = | |
if (size == max) IO.just(merged) | |
else IO.fx { | |
val head = rest.firstOrNull() | |
val tail = rest.drop(1) | |
if (prevChunks == head) !process(head, tail, merged, size, max) | |
else merge(prevChunks, merged) | |
} | |
fun merge(original: List<String>, chunks: List<String>): List<String> = | |
original + chunks // Mock impl, can also be `IO` | |
suspend fun main(): Unit = IO.fx { | |
val mergedLines = !process(emptyList(), chunks(), emptyList(), 0, 1000) | |
!effect { println(mergedLines) } | |
}.suspended() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment