The Aff idiocracy

Some Aff idioms which I want to share, since I think I’m getting accustomed to idiomatic Aff.

I would like to see other people’s Aff idioms. Like for example the docs for ParCont (which are not Aff, but same idea).

Concurrent side-effects

We have operation1, operation2, and operation3, which are all side-effecting Aff Unit operations. We want to start them all at the same time. We don’t know what order they’ll finish in, but we want to wait until they’re all finished before we continue.

  sequential $ traverse_ parallel
    [ do
        operation1
    , do
        operation2
    , do
        operation3
    ]

Concurrent effects with return values

We have operation1, operation2, and operation3, which are all side-effect Aff operations with return values. We want to start them all at the same time. We don’t know what order they’ll finish in, but we need all of their results before we can continue.

result1 /\ result2 /\ result3 <- sequential $ T3 <$>
  do parallel do
      operation1
  <*>
  do parallel do
      operation2
  <*>
  do parallel do
      operation3
4 Likes

Note that there is parSequence, and parTraverse, which cover 80% of ParAff use cases for me.
https://pursuit.purescript.org/packages/purescript-parallel/6.0.0/docs/Control.Parallel#v:parTraverse

7 Likes

Yeah, I see now that that first idiom Concurrent side-effects is parSequence_.

1 Like