-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRWS.hs
60 lines (52 loc) · 2.13 KB
/
RWS.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- | Examples of the RWS effect.
module Example.RWS where
-- base
import Data.Monoid (Sum(Sum))
-- hspec
import Test.Hspec (Spec, it, shouldBe)
-- effet
import Control.Effect.Identity
import Control.Effect.Reader
import Control.Effect.RWS
import Control.Effect.State
import qualified Control.Effect.State.Lazy as L
import qualified Control.Effect.Writer.Strict as S
import qualified Control.Effect.RWS.Lazy as L
import Example.Reader (triple)
import Example.State (increment)
import Example.Writer (sumWriter)
--- Example Programs -----------------------------------------------------------
-- | Combine reader, writer and state examples from the other test cases.
combineRWS :: RWS Int (Sum Int) Int m => m (Int, Int, Int)
combineRWS = do
r <- triple
w <- sumWriter
increment
s <- get
pure (r, w, s)
--- Test Cases -----------------------------------------------------------------
spec :: Spec
spec = do
it "tags and retags the effect" $
( runIdentity -- result: (Sum Int, Int, (Int, Int, Int))
. L.runRWS' @"b" 10 8 -- result: Monad m => m (Sum Int, Int, (Int, Int, Int))
. retagRWS' @"a" @"b" -- effects: RWS "b" Int (Sum Int) Int
. tagRWS' @"a" -- effects: RWS "a" Int (Sum Int) Int
$ combineRWS ) -- effects: RWS Int (Sum Int) Int
`shouldBe`
(Sum 18, 9, (30, 13, 9))
it "combines reader/write/state test cases" $
( runIdentity -- result: (Sum Int, Int, (Int, Int, Int))
. L.runRWS 20 5 -- result: Monad m => m (Sum Int, Int, (Int, Int, Int))
$ combineRWS ) -- effects: RWS Int (Sum Int) Int
`shouldBe`
(Sum 18, 6, (60, 13, 6))
it "separates reader/write/state components" $
( runIdentity -- result: (Int, (Sum Int, (Int, Int, Int)))
. L.runState 3 -- result: Monad m => m (Int, (Sum Int, (Int, Int, Int)))
. S.runWriter -- effects: State Int
. runReader 15 -- effects: Writer (Sum Int), State Int
. runSeparatedRWS -- effects: Reader Int, Writer (Sum Int), State Int
$ combineRWS ) -- effects: RWS Int (Sum Int) Int
`shouldBe`
(4, (Sum 18, (45, 13, 4)))