HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulerelude-1.2.2.2Haskell2010

Relude.Monad.Maybe

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

Utility functions to work with Maybe data type as monad.

Contents
  1. Combinators
  2. Monadic combinators
Synopsis
  • Packagerelude-1.2.2.2
  • Exports8
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMaybe.hs

Combinators

value(?:) :: Maybe a -> a -> a

Similar to fromMaybe but with flipped arguments.

Example1 expression
readMaybe "True" ?: FalseTrue
Example1 expression
readMaybe "Tru" ?: FalseFalse
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

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]