haskell-issues icon indicating copy to clipboard operation
haskell-issues copied to clipboard

transformers: Add modifyT and getsT

Open Gurkenglas opened this issue 8 years ago • 2 comments

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

Gurkenglas avatar Mar 19 '16 19:03 Gurkenglas

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

neongreen avatar Mar 19 '16 21:03 neongreen

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.

Gurkenglas avatar Mar 20 '16 03:03 Gurkenglas