HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerelude-1.2.0.0Haskell2010

Relude.Monad.Maybe

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

Utility functions to work with Relude.Maybe data type as monad.

  • 8 values
  • Packagerelude-1.2.0.0
  • Exports8
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMaybe.hs

Combinators

5 declarations
value(?:) :: Maybe a -> a -> a
#

Similar to fromMaybe but with flipped arguments.

Example1 expression
readMaybe "True" ?: FalseTrue
Example1 expression
readMaybe "Tru" ?: FalseFalse
valuewhenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()
#

Specialized version of Relude.for_ for Maybe. It's used for code readability.

Also helps to avoid space leaks: Foldable.mapM_ space leak.

Example2 expressions
whenJust Nothing $ \b -> print (not b)whenJust (Just True) $ \b -> print (not b)False
valuewhenJustM :: Monad m => m (Maybe a) -> (a -> m ()) -> m ()
#

Monadic version of whenJust.

Example2 expressions
whenJustM (pure Nothing) $ \b -> print (not b)whenJustM (pure $ Just True) $ \b -> print (not b)False
valuewhenNothing_ :: Applicative f => Maybe a -> f () -> f ()
#

Performs default Applicative action if Nothing is given. Do nothing for Just. Convenient for discarding Just content.

Example2 expressions
whenNothing_ Nothing $ putTextLn "Nothing!"Nothing!whenNothing_ (Just True) $ putTextLn "Nothing!"

Monadic combinators

3 declarations
valuewhenNothingM :: Monad m => m (Maybe a) -> m a -> m a
#

Monadic version of whenNothing.

Example2 expressions
whenNothingM (pure $ Just True) $ True <$ putTextLn "Is Just!"TruewhenNothingM (pure Nothing) $ False <$ putTextLn "Is Nothing!"Is Nothing!False
valuewhenNothingM_ :: Monad m => m (Maybe a) -> m () -> m ()
#

Monadic version of whenNothing_.

Example2 expressions
whenNothingM_ (pure $ Just True) $ putTextLn "Is Just!"whenNothingM_ (pure Nothing) $ putTextLn "Is Nothing!"Is Nothing!
valuemapMaybeM :: Monad m => (a -> m (Maybe b)) -> [a] -> m [b]
#

The monadic version of the mapMaybe function.

Example1 expression
:{evenInHalf :: Int -> IO (Maybe Int)evenInHalf n    | even n = pure $ Just $ n `div` 2    | otherwise = pure Nothing:}
Example1 expression
mapMaybeM evenInHalf [1..10][1,2,3,4,5]