Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Created November 9, 2019 09:44
Show Gist options
  • Select an option

  • Save nomisRev/66e71d9b05190a0168825c1eb94db1fc to your computer and use it in GitHub Desktop.

Select an option

Save nomisRev/66e71d9b05190a0168825c1eb94db1fc to your computer and use it in GitHub Desktop.
Impure Pics - Shared state in fp
fun add1(myState: Ref<ForIO, List<String>>): IO<Unit> =
IO.sleep(5.seconds).followedBy(myState.update { it + listOf("#1") })
fun add2(myState: Ref<ForIO, List<String>>): IO<Unit> =
IO.sleep(3.seconds).followedBy(myState.update { it + listOf("#2") })
fun read(myState: Ref<ForIO, List<String>>): IO<Unit> =
myState.get().effectMap { state -> println("$state") }
suspend fun main(): Unit = IO.fx {
val myState = !Ref(emptyList<String>())
!listOf(add1(myState), add2(myState)).parSequence()
!read(myState)
}.suspended()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment