-- |Misc combinators.
module Incipit.Misc where

import Incipit.Base

-- |Convenience alias for @pure ()@.
unit ::
  Applicative f =>
  f ()
unit :: forall (f :: * -> *). Applicative f => f ()
unit =
  forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# inline unit #-}

-- |Variant of 'when' that takes a monadic action for the condition.
whenM ::
  Monad m =>
  m Bool ->
  m () ->
  m ()
whenM :: forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM m Bool
condM m ()
action =
  m Bool
condM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ Bool
cond -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
cond m ()
action
{-# inline whenM #-}

-- |Variant of 'unless' that takes a monadic action for the condition.
unlessM ::
  Monad m =>
  m Bool ->
  m () ->
  m ()
unlessM :: forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM m Bool
condM m ()
action =
  m Bool
condM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ Bool
cond -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
cond m ()
action
{-# inline unlessM #-}

-- |Variant of @if@/@then@/@else@ that takes a monadic action for the condition and branches.
ifM ::
  Monad m =>
  m Bool ->
  m a ->
  m a ->
  m a
ifM :: forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
ifM m Bool
condM m a
onTrue m a
onFalse =
  m Bool
condM forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ Bool
cond -> if Bool
cond then m a
onTrue else m a
onFalse
{-# inline ifM #-}