Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Created March 27, 2020 10:23
Show Gist options
  • Save nomisRev/09532c58e222379ca17501b246a96d1d to your computer and use it in GitHub Desktop.
Save nomisRev/09532c58e222379ca17501b246a96d1d to your computer and use it in GitHub Desktop.
Process chunks with IO - Question on KotlinLang Slack
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