universum-0.2.1: src/Monad/Maybe.hs
{-# LANGUAGE Safe #-}
-- | Utility functions to work with 'Data.Maybe' data type as monad.
module Monad.Maybe
( whenJust
, whenJustM
, whenNothing
, whenNothing_
, whenNothingM
, whenNothingM_
) where
import Control.Applicative (Applicative, pure)
import Control.Monad (Monad (..))
import Data.Maybe (Maybe (..))
import Applicative (pass)
whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()
whenJust (Just x) f = f x
whenJust Nothing _ = pass
{-# INLINE whenJust #-}
whenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
whenJustM mm f = mm >>= \m -> whenJust m f
{-# INLINE whenJustM #-}
whenNothing :: Applicative f => Maybe a -> f a -> f a
whenNothing (Just x) _ = pure x
whenNothing Nothing m = m
{-# INLINE whenNothing #-}
whenNothing_ :: Applicative f => Maybe a -> f () -> f ()
whenNothing_ Nothing m = m
whenNothing_ _ _ = pass
{-# INLINE whenNothing_ #-}
whenNothingM :: Monad m => m (Maybe a) -> m a -> m a
whenNothingM mm action = mm >>= \m -> whenNothing m action
{-# INLINE whenNothingM #-}
whenNothingM_ :: Monad m => m (Maybe a) -> m () -> m ()
whenNothingM_ mm action = mm >>= \m -> whenNothing_ m action
{-# INLINE whenNothingM_ #-}