on master help sign in
More State-related functions
commitfdd32bdf008c627344b7b010ff155489947f2761 authorIndex Int <vlad.z.4096@gmail.com> dateWed 10 Jun 2015, 20:33 (11 years ago) parentac4129c
2 files changed+48
src/Control/Monad/Ether/State.hs+16 src/Control/Monad/Trans/Ether/State.hs+32
src/Control/Monad/Ether/State.hs+16
@@ -17,20 +17,30 @@ module Control.Monad.Ether.State
1717 , State'
1818 , runEtherStateT
1919 , runEtherState
20+ , evalEtherStateT
21+ , evalEtherState
22+ , execEtherStateT
23+ , execEtherState
2024 , runStateT'
2125 , runState'
26+ , evalStateT'
27+ , evalState'
28+ , execStateT'
29+ , execState'
2230 --
2331 , MonadEtherState
2432 , etherGet
2533 , etherGets
2634 , etherPut
2735 , etherState
36+ , etherModify
2837 --
2938 , MonadState'
3039 , get'
3140 , gets'
3241 , put'
3342 , state'
43+ , modify'
3444 ) where
3545
3646import Data.Proxy (Proxy(Proxy))
@@ -71,6 +81,9 @@ class Monad m => MonadEtherState tag s m | m tag -> s where
7181etherGets :: MonadEtherState tag s m => proxy tag -> (s -> a) -> m a
7282etherGets proxy f = fmap f (etherGet proxy)
7383
84+etherModify :: MonadEtherState tag s m => proxy tag -> (s -> s) -> m ()
85+etherModify proxy f = etherState proxy $ \ s -> ((), f s)
86+
7487type MonadState' s = MonadEtherState s s
7588
7689get' :: forall m s . MonadState' s m => m s
@@ -85,6 +98,9 @@ put' = etherPut (Proxy :: Proxy s)
8598state' :: forall m s a . MonadState' s m => (s -> (a, s)) -> m a
8699state' = etherState (Proxy :: Proxy s)
87100
101+modify' :: forall m s . MonadState' s m => (s -> s) -> m ()
102+modify' = etherModify (Proxy :: Proxy s)
103+
88104instance {-# OVERLAPPING #-} Monad m => MonadEtherState tag s (EtherStateT tag s m) where
89105 etherGet proxy = etherStateT proxy (\s -> return (s, s))
90106 etherPut proxy s = etherStateT proxy (\_ -> return ((), s))
src/Control/Monad/Trans/Ether/State.hs+32
@@ -13,8 +13,16 @@ module Control.Monad.Trans.Ether.State
1313 , State'
1414 , runEtherStateT
1515 , runEtherState
16+ , evalEtherStateT
17+ , evalEtherState
18+ , execEtherStateT
19+ , execEtherState
1620 , runStateT'
1721 , runState'
22+ , evalStateT'
23+ , evalState'
24+ , execStateT'
25+ , execState'
1826 --
1927 , etherStateT
2028 , mapEtherStateT
@@ -60,6 +68,18 @@ runEtherStateT _proxy (EtherStateT (S.StateT f)) = f
6068runEtherState :: proxy tag -> EtherState tag s a -> s -> (a, s)
6169runEtherState proxy m s = runIdentity (runEtherStateT proxy m s)
6270
71+evalEtherStateT :: Functor m => proxy tag -> EtherStateT tag s m a -> s -> m a
72+evalEtherStateT proxy m s = fmap fst (runEtherStateT proxy m s)
73+
74+evalEtherState :: proxy tag -> EtherState tag s a -> s -> a
75+evalEtherState proxy m s = fst (runEtherState proxy m s)
76+
77+execEtherStateT :: Functor m => proxy tag -> EtherStateT tag s m a -> s -> m s
78+execEtherStateT proxy m s = fmap snd (runEtherStateT proxy m s)
79+
80+execEtherState :: proxy tag -> EtherState tag s a -> s -> s
81+execEtherState proxy m s = snd (runEtherState proxy m s)
82+
6383mapEtherStateT :: proxy tag -> (m (a, s) -> n (b, s)) -> EtherStateT tag s m a -> EtherStateT tag s n b
6484mapEtherStateT _proxy f m = EtherStateT $ S.mapStateT f (coerce m)
6585
@@ -84,6 +104,18 @@ runStateT' = runEtherStateT Proxy
84104runState' :: State' s a -> s -> (a, s)
85105runState' m s = runIdentity (runStateT' m s)
86106
107+evalStateT' :: Functor m => StateT' s m a -> s -> m a
108+evalStateT' m s = fmap fst (runStateT' m s)
109+
110+evalState' :: State' s a -> s -> a
111+evalState' m s = fst (runState' m s)
112+
113+execStateT' :: Functor m => StateT' s m a -> s -> m s
114+execStateT' m s = fmap snd (runStateT' m s)
115+
116+execState' :: State' s a -> s -> s
117+execState' m s = snd (runState' m s)
118+
87119-- Instances for mtl classes
88120
89121instance Class.MonadCont m => Class.MonadCont (EtherStateT tag s m) where