Make “sublibrary” standard terminology in docs
[cabal.git] / Cabal / src / Distribution / Utils / MapAccum.hs
blob31f62d7d02f2ea3e2994cc78523021dc310f4f85
1 {-# LANGUAGE CPP #-}
3 module Distribution.Utils.MapAccum (mapAccumM) where
5 import Distribution.Compat.Prelude
6 import Prelude ()
8 -- Like StateT but with return tuple swapped
9 newtype StateM s m a = StateM {runStateM :: s -> m (s, a)}
11 instance Functor m => Functor (StateM s m) where
12 fmap f (StateM x) = StateM $ \s -> fmap (\(s', a) -> (s', f a)) (x s)
14 instance Monad m => Applicative (StateM s m) where
15 pure x = StateM $ \s -> return (s, x)
16 StateM f <*> StateM x = StateM $ \s -> do
17 (s', f') <- f s
18 (s'', x') <- x s'
19 return (s'', f' x')
21 -- | Monadic variant of 'mapAccumL'.
22 mapAccumM
23 :: (Monad m, Traversable t)
24 => (a -> b -> m (a, c))
25 -> a
26 -> t b
27 -> m (a, t c)
28 mapAccumM f s t = runStateM (traverse (\x -> StateM (\s' -> f s' x)) t) s