/
Internal.purs
44 lines (32 loc) · 1.3 KB
/
Internal.purs
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
module Data.Traversable.Accum.Internal
( StateL(..)
, stateL
, StateR(..)
, stateR
) where
import Prelude
import Data.Traversable.Accum (Accum)
newtype StateL s a = StateL (s -> Accum s a)
stateL :: forall s a. StateL s a -> s -> Accum s a
stateL (StateL k) = k
instance functorStateL :: Functor (StateL s) where
map f k = StateL \s -> case stateL k s of
{ accum: s1, value: a } -> { accum: s1, value: f a }
instance applyStateL :: Apply (StateL s) where
apply f x = StateL \s -> case stateL f s of
{ accum: s1, value: f' } -> case stateL x s1 of
{ accum: s2, value: x' } -> { accum: s2, value: f' x' }
instance applicativeStateL :: Applicative (StateL s) where
pure a = StateL \s -> { accum: s, value: a }
newtype StateR s a = StateR (s -> Accum s a)
stateR :: forall s a. StateR s a -> s -> Accum s a
stateR (StateR k) = k
instance functorStateR :: Functor (StateR s) where
map f k = StateR \s -> case stateR k s of
{ accum: s1, value: a } -> { accum: s1, value: f a }
instance applyStateR :: Apply (StateR s) where
apply f x = StateR \s -> case stateR x s of
{ accum: s1, value: x' } -> case stateR f s1 of
{ accum: s2, value: f' } -> { accum: s2, value: f' x' }
instance applicativeStateR :: Applicative (StateR s) where
pure a = StateR \s -> { accum: s, value: a }