3 module Distribution
.Utils
.MapAccum
(mapAccumM
) where
5 import Distribution
.Compat
.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
21 -- | Monadic variant of 'mapAccumL'.
23 :: (Monad m
, Traversable t
)
24 => (a
-> b
-> m
(a
, c
))
28 mapAccumM f s t
= runStateM
(traverse
(\x
-> StateM
(\s
' -> f s
' x
)) t
) s