{-# LANGUAGE Safe #-}
module Relude.Monad.Maybe
(
(?:)
, whenJust
, whenJustM
, whenNothing
, whenNothing_
, whenNothingM
, whenNothingM_
, mapMaybeM
) where
import Relude.Applicative (Applicative, pass, pure)
import Relude.Foldable.Reexport (mapM)
import Relude.Function ((.))
import Relude.Functor.Reexport (fmap)
import Relude.Monad.Reexport (Maybe (..), Monad (..), catMaybes, fromMaybe)
infixr 0 ?:
(?:) :: Maybe a -> a -> a
mA :: Maybe a
mA ?: :: Maybe a -> a -> a
?: b :: 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 x :: a
x) f :: a -> f ()
f = a -> f ()
f a
x
whenJust Nothing _ = 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 mm :: m (Maybe a)
mm f :: 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
>>= \m :: 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 x :: a
x) _ = a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
whenNothing Nothing m :: f a
m = f a
m
{-# INLINE whenNothing #-}
whenNothing_ :: Applicative f => Maybe a -> f () -> f ()
whenNothing_ :: Maybe a -> f () -> f ()
whenNothing_ Nothing m :: f ()
m = f ()
m
whenNothing_ _ _ = 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 mm :: m (Maybe a)
mm action :: 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
>>= \m :: 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_ mm :: m (Maybe a)
mm action :: 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
>>= \m :: Maybe a
m -> Maybe a -> m () -> m ()
forall (f :: * -> *) a. Applicative f => Maybe a -> f () -> f ()
whenNothing_ Maybe a
m m ()
action
{-# INLINE whenNothingM_ #-}
mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM :: (a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM f :: a -> m (Maybe b)
f = ([Maybe b] -> [b]) -> m [Maybe b] -> m [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe b] -> [b]
forall a. [Maybe a] -> [a]
catMaybes (m [Maybe b] -> m [b]) -> ([a] -> m [Maybe b]) -> [a] -> m [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (Maybe b)) -> [a] -> m [Maybe b]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM a -> m (Maybe b)
f
{-# INLINE mapMaybeM #-}