-- | Monad functions.
module Music.Theory.Monad where

-- | 'sequence_' of 'repeat'.
repeatM_ :: Monad m => m a -> m ()
repeatM_ :: forall (m :: * -> *) a. Monad m => m a -> m ()
repeatM_ = forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a]
repeat

-- | Monadic variant of 'iterate'.
iterateM_ :: Monad m => (st -> m st) -> st -> m ()
iterateM_ :: forall (m :: * -> *) st. Monad m => (st -> m st) -> st -> m ()
iterateM_ st -> m st
f st
st = do
  st
st' <- st -> m st
f st
st
  forall (m :: * -> *) st. Monad m => (st -> m st) -> st -> m ()
iterateM_ st -> m st
f st
st'

-- | 'fmap' of 'concat' of 'mapM'
concatMapM :: (Functor m, Monad m) => (t -> m [u]) -> [t] -> m [u]
concatMapM :: forall (m :: * -> *) t u.
(Functor m, Monad m) =>
(t -> m [u]) -> [t] -> m [u]
concatMapM t -> m [u]
f = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM t -> m [u]
f

-- | If i then j else k.
m_if :: Monad m => (m Bool,m t,m t) -> m t
m_if :: forall (m :: * -> *) t. Monad m => (m Bool, m t, m t) -> m t
m_if (m Bool
i,m t
j,m t
k) = do
  Bool
r <- m Bool
i
  if Bool
r then m t
j else m t
k