aelve / haskell-issues

An unofficial issue tracker for all things Haskell-related
18 stars 0 forks source link

transformers: Add modifyT and getsT #2

Open Gurkenglas opened 8 years ago

Gurkenglas commented 8 years ago

These rectangles deserve to be completed:

execState :: State s () -> (s -> s)              modify :: (s -> s) -> State s ()

execStateT :: StateT s m () -> (s -> m s)        modifyT :: (s -> m s) -> StateT s m ()

evalState :: State s a -> (s -> a)               gets :: (s -> a) -> State s a

evalStateT :: StateT s m a -> (s -> m a)         getsT :: (s -> m a) -> StateT s m a

The implementations would be something like:

modifyT f = StateT $ \s -> ((),) <$> f s
getsT f = StateT $ \s -> (,s) <$> f s
neongreen commented 8 years ago

Could you elaborate on how they are useful here? (I looked thru the #haskell archives, but having a coherent example here would be better.)

Gurkenglas commented 8 years ago

I use them pretty often, lots for example on http://lpaste.net/148381 (You might find some more on https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:lpaste.net+modifyT ).

Have you ever used StateT as a function? The use cases are similar, and the rectangle runState-state-runStateT-StateT is equivalent to the above.