{-# LANGUAGE Safe #-}
module Universum.Monad.Maybe
( (?:)
, whenJust
, whenJustM
, whenNothing
, whenNothing_
, whenNothingM
, whenNothingM_
) where
import Universum.Applicative (Applicative, pass, pure)
import Universum.Monad.Reexport (Maybe (..), Monad (..), fromMaybe)
infixr 0 ?:
(?:) :: Maybe a -> a -> a
Maybe a
mA ?: :: Maybe a -> a -> a
?: a
b = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
b Maybe a
mA
{-# INLINE (?:) #-}
whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()
whenJust :: Maybe a -> (a -> f ()) -> f ()
whenJust (Just a
x) a -> f ()
f = a -> f ()
f a
x
whenJust Maybe a
Nothing a -> f ()
_ = f ()
forall (f :: * -> *). Applicative f => f ()
pass
{-# INLINE whenJust #-}
whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
whenJustM :: m (Maybe a) -> (a -> m ()) -> m ()
whenJustM m (Maybe a)
mm a -> m ()
f = m (Maybe a)
mm m (Maybe a) -> (Maybe a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe a
m -> Maybe a -> (a -> m ()) -> m ()
forall (f :: * -> *) a.
Applicative f =>
Maybe a -> (a -> f ()) -> f ()
whenJust Maybe a
m a -> m ()
f
{-# INLINE whenJustM #-}
whenNothing :: Applicative f => Maybe a -> f a -> f a
whenNothing :: Maybe a -> f a -> f a
whenNothing (Just a
x) f a
_ = a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
whenNothing Maybe a
Nothing f a
m = f a
m
{-# INLINE whenNothing #-}
whenNothing_ :: Applicative f => Maybe a -> f () -> f ()
whenNothing_ :: Maybe a -> f () -> f ()
whenNothing_ Maybe a
Nothing f ()
m = f ()
m
whenNothing_ Maybe a
_ f ()
_ = f ()
forall (f :: * -> *). Applicative f => f ()
pass
{-# INLINE whenNothing_ #-}
whenNothingM :: Monad m => m (Maybe a) -> m a -> m a
whenNothingM :: m (Maybe a) -> m a -> m a
whenNothingM m (Maybe a)
mm m a
action = m (Maybe a)
mm m (Maybe a) -> (Maybe a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe a
m -> Maybe a -> m a -> m a
forall (f :: * -> *) a. Applicative f => Maybe a -> f a -> f a
whenNothing Maybe a
m m a
action
{-# INLINE whenNothingM #-}
whenNothingM_ :: Monad m => m (Maybe a) -> m () -> m ()
whenNothingM_ :: m (Maybe a) -> m () -> m ()
whenNothingM_ m (Maybe a)
mm m ()
action = m (Maybe a)
mm m (Maybe a) -> (Maybe a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe a
m -> Maybe a -> m () -> m ()
forall (f :: * -> *) a. Applicative f => Maybe a -> f () -> f ()
whenNothing_ Maybe a
m m ()
action
{-# INLINE whenNothingM_ #-}