Skip to content

Commit

Permalink
add Lazy instance for Aff (#142)
Browse files Browse the repository at this point in the history
* add Lazy instance for Aff

* change Lazy tests so result is returned from fix

* move test_lazy after kill and avar tests
  • Loading branch information
safareli authored and natefaubion committed Apr 6, 2018
1 parent aaf98f9 commit 34a038b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Control/Monad/Aff.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Prelude
import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Apply (lift2)
import Control.Lazy (class Lazy)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Class (class MonadEff, liftEff)
import Control.Monad.Eff.Exception (Error, EXCEPTION, error)
Expand Down Expand Up @@ -110,6 +111,9 @@ instance monadErrorAff ∷ MonadError Error (Aff eff) where
instance monadEffAffMonadEff eff (Aff eff) where
liftEff = _liftEff

instance lazyAffLazy (Aff eff a) where
defer f = pure unit >>= f

-- | Applicative for running parallel effects. Any `Aff` can be coerced to a
-- | `ParAff` and back using the `Parallel` class.
foreign import data ParAff # Effect Type Type
Expand Down
22 changes: 22 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Test.Main where
import Prelude

import Control.Alt ((<|>))
import Control.Lazy (fix)
import Control.Monad.Aff (Aff, Canceler(..), runAff_, launchAff, makeAff, try, bracket, generalBracket, delay, forkAff, suspendAff, joinFiber, killFiber, never, supervise, Error, error, message)
import Control.Monad.Aff.AVar (AVAR, makeEmptyVar, takeVar, putVar)
import Control.Monad.Aff.Compat as AC
Expand Down Expand Up @@ -632,6 +633,26 @@ test_scheduler_size = assert "scheduler" do
_ ← traverse joinFiber =<< traverse forkAff (Array.replicate 100000 (modifyRef ref (add 1)))
eq 100000 <$> readRef ref

test_lazy eff. TestAff eff Unit
test_lazy = assert "Lazy Aff" do
varA ← makeEmptyVar
varB ← makeEmptyVar
fiberA <- forkAff $ fix \loop -> do
a <- takeVar varA
putVar (a + 1) varB
loop
fiberB <- forkAff $ fix \loop -> do
b <- takeVar varB
if (b > 100)
then do
killFiber (error "finished") fiberA
pure "done"
else do
putVar (b + 1) varA
loop
putVar 0 varA
eq "done" <$> joinFiber fiberB

main TestEff () Unit
main = do
test_pure
Expand Down Expand Up @@ -670,6 +691,7 @@ main = do
test_kill_parallel_alt
test_kill_parallel_alt_finalizer
test_avar_order
test_lazy
test_efffn
test_fiber_map
test_fiber_apply
Expand Down

0 comments on commit 34a038b

Please sign in to comment.