Add state and stateM variants of countdown

This commit is contained in:
Andrzej Rybczak 2022-08-12 15:02:17 +02:00
parent 01e11afc02
commit 870660445d
2 changed files with 52 additions and 0 deletions

View File

@ -133,6 +133,50 @@ countdownEffectfulLocalDeep n = E.runPureEff
where
runR = E.runReader ()
----
programEffectfulLocalSt :: EL.State Integer E.:> es => E.Eff es Integer
programEffectfulLocalSt = do
n <- EL.state @Integer $ \s -> (s, s - 1)
if n <= 0
then pure n
else programEffectfulLocalSt
{-# NOINLINE programEffectfulLocalSt #-}
countdownEffectfulLocalSt :: Integer -> (Integer, Integer)
countdownEffectfulLocalSt n = E.runPureEff . EL.runState n $ programEffectfulLocalSt
countdownEffectfulLocalDeepSt :: Integer -> (Integer, Integer)
countdownEffectfulLocalDeepSt n = E.runPureEff
. runR . runR . runR . runR . runR
. EL.runState n
. runR . runR . runR . runR . runR
$ programEffectfulLocalSt
where
runR = E.runReader ()
----
programEffectfulLocalStM :: EL.State Integer E.:> es => E.Eff es Integer
programEffectfulLocalStM = do
n <- EL.stateM @Integer $ \s -> pure (s, s - 1)
if n <= 0
then pure n
else programEffectfulLocalStM
{-# NOINLINE programEffectfulLocalStM #-}
countdownEffectfulLocalStM :: Integer -> (Integer, Integer)
countdownEffectfulLocalStM n = E.runPureEff . EL.runState n $ programEffectfulLocalStM
countdownEffectfulLocalDeepStM :: Integer -> (Integer, Integer)
countdownEffectfulLocalDeepStM n = E.runPureEff
. runR . runR . runR . runR . runR
. EL.runState n
. runR . runR . runR . runR . runR
$ programEffectfulLocalStM
where
runR = E.runReader ()
----------------------------------------
-- effectful (mvar)

View File

@ -31,6 +31,14 @@ countdown n = bgroup (show n)
[ bench "shallow" $ nf countdownEffectfulLocal n
, bench "deep" $ nf countdownEffectfulLocalDeep n
]
, bgroup "effectful (local/static/state)"
[ bench "shallow" $ nf countdownEffectfulLocalSt n
, bench "deep" $ nf countdownEffectfulLocalDeepSt n
]
, bgroup "effectful (local/static/stateM)"
[ bench "shallow" $ nf countdownEffectfulLocalStM n
, bench "deep" $ nf countdownEffectfulLocalDeepStM n
]
, bgroup "effectful (local/dynamic)"
[ bench "shallow" $ nf countdownEffectfulDynLocal n
, bench "deep" $ nf countdownEffectfulDynLocalDeep n