Created
May 30, 2020 07:13
-
-
Save mgryszko/5938a4727af82691290d07348509ce9c to your computer and use it in GitHub Desktop.
Arrow ParApplicative mapN combining effects sequentially (unlike parMapN)
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.io.concurrent.concurrent | |
import arrow.fx.extensions.io.concurrent.dispatchers | |
import arrow.fx.fix | |
import kotlin.test.Test | |
fun f1(): IO<String> = IO { | |
"f1 ${Thread.currentThread().name}" | |
} | |
fun f2(): IO<String> = IO { | |
"f2 ${Thread.currentThread().name}" | |
} | |
fun parApplicativeMapN(): IO<String> = | |
IO.concurrent().parApplicative(dispatchers().default()).run { | |
mapN(f1(), f2()) { (log1, log2) -> | |
""" | |
$log1 | |
$log2 | |
${Thread.currentThread().name} | |
""".trimIndent() | |
} | |
}.fix() | |
fun parApplicativeMap2(): IO<String> = | |
IO.concurrent().parApplicative(dispatchers().default()).run { | |
f1().map2(f2()) { (log1, log2) -> | |
""" | |
$log1 | |
$log2 | |
${Thread.currentThread().name} | |
""".trimIndent() | |
} | |
}.fix() | |
class ParApplicativeTest { | |
@Test | |
fun mapN() { | |
println(parApplicativeMapN().unsafeRunSync()) | |
} | |
@Test | |
fun map2() { | |
println(parApplicativeMap2().unsafeRunSync()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment