mirror of
https://github.com/haskell-effectful/effectful.git
synced 2024-11-23 14:36:11 +03:00
Add state and stateM variants of countdown
This commit is contained in:
parent
01e11afc02
commit
870660445d
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user