Skip to content

Commit 34a038b

Browse files
safarelinatefaubion
authored andcommitted
add Lazy instance for Aff (#142)
* add Lazy instance for Aff * change Lazy tests so result is returned from fix * move test_lazy after kill and avar tests
1 parent aaf98f9 commit 34a038b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Control/Monad/Aff.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Prelude
3636
import Control.Alt (class Alt)
3737
import Control.Alternative (class Alternative)
3838
import Control.Apply (lift2)
39+
import Control.Lazy (class Lazy)
3940
import Control.Monad.Eff (Eff, kind Effect)
4041
import Control.Monad.Eff.Class (class MonadEff, liftEff)
4142
import Control.Monad.Eff.Exception (Error, EXCEPTION, error)
@@ -110,6 +111,9 @@ instance monadErrorAff ∷ MonadError Error (Aff eff) where
110111
instance monadEffAffMonadEff eff (Aff eff) where
111112
liftEff = _liftEff
112113

114+
instance lazyAffLazy (Aff eff a) where
115+
defer f = pure unit >>= f
116+
113117
-- | Applicative for running parallel effects. Any `Aff` can be coerced to a
114118
-- | `ParAff` and back using the `Parallel` class.
115119
foreign import data ParAff ∷ # Effect Type Type

test/Test/Main.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Test.Main where
33
import Prelude
44

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

636+
test_lazy eff. TestAff eff Unit
637+
test_lazy = assert "Lazy Aff" do
638+
varA ← makeEmptyVar
639+
varB ← makeEmptyVar
640+
fiberA <- forkAff $ fix \loop -> do
641+
a <- takeVar varA
642+
putVar (a + 1) varB
643+
loop
644+
fiberB <- forkAff $ fix \loop -> do
645+
b <- takeVar varB
646+
if (b > 100)
647+
then do
648+
killFiber (error "finished") fiberA
649+
pure "done"
650+
else do
651+
putVar (b + 1) varA
652+
loop
653+
putVar 0 varA
654+
eq "done" <$> joinFiber fiberB
655+
635656
main TestEff () Unit
636657
main = do
637658
test_pure
@@ -670,6 +691,7 @@ main = do
670691
test_kill_parallel_alt
671692
test_kill_parallel_alt_finalizer
672693
test_avar_order
694+
test_lazy
673695
test_efffn
674696
test_fiber_map
675697
test_fiber_apply

0 commit comments

Comments
 (0)