Skip to content

Instantly share code, notes, and snippets.

@mgryszko
Created May 30, 2020 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgryszko/5938a4727af82691290d07348509ce9c to your computer and use it in GitHub Desktop.
Save mgryszko/5938a4727af82691290d07348509ce9c to your computer and use it in GitHub Desktop.
Arrow ParApplicative mapN combining effects sequentially (unlike parMapN)
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