Commit 5c427e82 authored by Kai-Oliver Prott's avatar Kai-Oliver Prott
Browse files

Move mapAccumM and concatMapM to Control.Monad.Extra

parent 88d08a2c
module Control.Monad.Extra where
--- Same as `concatMap`, but for a monadic function.
concatMapM :: (Functor m, Monad m) => (a -> m [b]) -> [a] -> m [b]
concatMapM f xs = concat <$> mapM f xs
--- Same as `mapM` but with an additional accumulator threaded through.
mapAccumM :: Monad m => (a -> b -> m (a, c))
-> a -> [b] -> m (a, [c])
mapAccumM _ s [] = return (s, [])
mapAccumM f s (x : xs) = f s x >>= (\(s', x') -> (mapAccumM f s' xs) >>=
(\(s'', xs') -> return (s'', x' : xs')))
......@@ -69,14 +69,3 @@ catchError m h = ErrorT (do a <- runErrorT m
runError :: ErrorT e Identity a -> Either e a
runError = runIdentity . runErrorT
--- Same as `concatMap`, but for a monadic function.
concatMapM :: (Error e, Functor m, Monad m) => (a -> ErrorT e m [b])
-> [a] -> ErrorT e m [b]
concatMapM f xs = concat <$> mapM f xs
--- Same as `mapM` but with an additional accumulator threaded through.
mapAccumM :: (Error e, Monad m) => (a -> b -> ErrorT e m (a, c))
-> a -> [b] -> ErrorT e m (a, [c])
mapAccumM _ s [] = return (s, [])
mapAccumM f s (x : xs) = f s x >>= (\(s', x') -> (mapAccumM f s' xs) >>=
(\(s'', xs') -> return (s'', x' : xs')))
......@@ -9,9 +9,8 @@
module Control.Monad.Trans.State
( State, StateT(runStateT)
, evalStateT, execStateT
, get, put, modify, sequence, sequence_, mapM
, mapM_, runState, evalState, execState, liftM, liftM2,
module Control.Monad.Trans.Class
, get, put, modify, runState, evalState, execState
, module Control.Monad.Trans.Class
) where
import Control.Monad.Trans.Class
......@@ -71,3 +70,4 @@ execStateT :: (Monad m) => StateT s m a -> s -> m s
execStateT m s = do
~(_, s') <- runStateT m s
return s'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment