>>> maybe False odd (Just 3) True
>>> maybe False odd Nothing FalseRead an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import GHC.Internal.Text.Read ( readMaybe ) >>> maybe 0 (*2) (readMaybe "5") 10 >>> maybe 0 (*2) (readMaybe "") 0Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5) "5" >>> maybe "" show Nothing ""
>>> maybe False odd (Just 3) True
>>> maybe False odd Nothing FalseRead an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe ) >>> maybe 0 (*2) (readMaybe "5") 10 >>> maybe 0 (*2) (readMaybe "") 0Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5) "5" >>> maybe "" show Nothing ""
>>> input (maybe natural) "Some 1" Just 1
>>> maybe False odd (Just 3) True
>>> maybe False odd Nothing FalseRead an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe ) >>> maybe 0 (*2) (readMaybe "5") 10 >>> maybe 0 (*2) (readMaybe "") 0Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5) "5" >>> maybe "" show Nothing ""
>>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right >>> maybe f = Parser.either (toEither . f)
>>> maybe f = Parser.fromFoldMaybe "maybe: predicate failed" (Fold.maybe f)Pre-release
>>> maybe 0 (`sMod` 2) (sJust (3 :: SInteger)) 1 :: SInteger >>> maybe 0 (`sMod` 2) (sNothing :: SMaybe Integer) 0 :: SInteger >>> let f = uninterpret "f" :: SInteger -> SBool >>> prove $ \x d -> maybe d f (sJust x) .== f x Q.E.D. >>> prove $ \d -> maybe d f sNothing .== d Q.E.D.
>>> maybe False odd (Just 3) True
>>> maybe False odd Nothing FalseRead an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe ) >>> maybe 0 (*2) (readMaybe "5") 10 >>> maybe 0 (*2) (readMaybe "") 0Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5) "5" >>> maybe "" show Nothing ""
>>> decode (maybe string) "42" Just Nothing >>> decode (maybe int) "42" Just (Just 42)
pure f <*> pure x = pure (f x) -- must hold for all fThis law does not hold for the expected applicative functor instance of Maybe, as this instance does not satisfy pure f <*> pure _|_ = pure (f _|_) for f = const.