precursor-0.1.0.0: Prelude replacement

Precursor.Data.Maybe

Synopsis

# Documentation

data Maybe a :: * -> * #

The Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error.

The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type.

Constructors

 Nothing Just a

Instances

 Methods(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b #(>>) :: Maybe a -> Maybe b -> Maybe b #return :: a -> Maybe a #fail :: String -> Maybe a # Methodsfmap :: (a -> b) -> Maybe a -> Maybe b #(<\$) :: a -> Maybe b -> Maybe a # Methodsmfix :: (a -> Maybe a) -> Maybe a # Methodspure :: a -> Maybe a #(<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b #(*>) :: Maybe a -> Maybe b -> Maybe b #(<*) :: Maybe a -> Maybe b -> Maybe a # Methodsfold :: Monoid m => Maybe m -> m #foldMap :: Monoid m => (a -> m) -> Maybe a -> m #foldr :: (a -> b -> b) -> b -> Maybe a -> b #foldr' :: (a -> b -> b) -> b -> Maybe a -> b #foldl :: (b -> a -> b) -> b -> Maybe a -> b #foldl' :: (b -> a -> b) -> b -> Maybe a -> b #foldr1 :: (a -> a -> a) -> Maybe a -> a #foldl1 :: (a -> a -> a) -> Maybe a -> a #toList :: Maybe a -> [a] #null :: Maybe a -> Bool #length :: Maybe a -> Int #elem :: Eq a => a -> Maybe a -> Bool #maximum :: Ord a => Maybe a -> a #minimum :: Ord a => Maybe a -> a #sum :: Num a => Maybe a -> a #product :: Num a => Maybe a -> a # Methodstraverse :: Applicative f => (a -> f b) -> Maybe a -> f (Maybe b) #sequenceA :: Applicative f => Maybe (f a) -> f (Maybe a) #mapM :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b) #sequence :: Monad m => Maybe (m a) -> m (Maybe a) # Associated Typestype Rep1 (Maybe :: * -> *) :: * -> * # Methodsfrom1 :: Maybe a -> Rep1 Maybe a #to1 :: Rep1 Maybe a -> Maybe a # MethodsliftEq :: (a -> b -> Bool) -> Maybe a -> Maybe b -> Bool # MethodsliftCompare :: (a -> b -> Ordering) -> Maybe a -> Maybe b -> Ordering # MethodsliftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Maybe a) #liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Maybe a] # MethodsliftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Maybe a -> ShowS #liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Maybe a] -> ShowS # Methodsempty :: Maybe a #(<|>) :: Maybe a -> Maybe a -> Maybe a #some :: Maybe a -> Maybe [a] #many :: Maybe a -> Maybe [a] # Methodsmzero :: Maybe a #mplus :: Maybe a -> Maybe a -> Maybe a # Eq a => Eq (Maybe a) Methods(==) :: Maybe a -> Maybe a -> Bool #(/=) :: Maybe a -> Maybe a -> Bool # Ord a => Ord (Maybe a) Methodscompare :: Maybe a -> Maybe a -> Ordering #(<) :: Maybe a -> Maybe a -> Bool #(<=) :: Maybe a -> Maybe a -> Bool #(>) :: Maybe a -> Maybe a -> Bool #(>=) :: Maybe a -> Maybe a -> Bool #max :: Maybe a -> Maybe a -> Maybe a #min :: Maybe a -> Maybe a -> Maybe a # Generic (Maybe a) Associated Typestype Rep (Maybe a) :: * -> * # Methodsfrom :: Maybe a -> Rep (Maybe a) x #to :: Rep (Maybe a) x -> Maybe a # Semigroup a => Semigroup (Maybe a) Methods(<>) :: Maybe a -> Maybe a -> Maybe a #sconcat :: NonEmpty (Maybe a) -> Maybe a #stimes :: Integral b => b -> Maybe a -> Maybe a # Monoid a => Monoid (Maybe a) Lift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e*e = e and e*s = s = s*e for all s ∈ S." Since there is no "Semigroup" typeclass providing just mappend, we use Monoid instead. Methodsmempty :: Maybe a #mappend :: Maybe a -> Maybe a -> Maybe a #mconcat :: [Maybe a] -> Maybe a # Lift a => Lift (Maybe a) Methodslift :: Maybe a -> Q Exp # SingI (Maybe a) (Nothing a) Methodssing :: Sing (Nothing a) a SingKind a (KProxy a) => SingKind (Maybe a) (KProxy (Maybe a)) Associated Typestype DemoteRep (KProxy (Maybe a)) (kparam :: KProxy (KProxy (Maybe a))) :: * MethodsfromSing :: Sing (KProxy (Maybe a)) a -> DemoteRep (KProxy (Maybe a)) kparam SingI a a1 => SingI (Maybe a) (Just a a1) Methodssing :: Sing (Just a a1) a type Rep1 Maybe type Rep1 Maybe = D1 (MetaData "Maybe" "GHC.Base" "base" False) ((:+:) (C1 (MetaCons "Nothing" PrefixI False) U1) (C1 (MetaCons "Just" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1))) type Rep (Maybe a) type Rep (Maybe a) = D1 (MetaData "Maybe" "GHC.Base" "base" False) ((:+:) (C1 (MetaCons "Nothing" PrefixI False) U1) (C1 (MetaCons "Just" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)))) data Sing (Maybe a) data Sing (Maybe a) whereSNothing :: Sing (Maybe a) (Nothing a)SJust :: Sing (Maybe a) (Just a a1) type (==) (Maybe k) a b type (==) (Maybe k) a b = EqMaybe k a b type DemoteRep (Maybe a) (KProxy (Maybe a)) type DemoteRep (Maybe a) (KProxy (Maybe a)) = Maybe (DemoteRep a (KProxy a))

maybe :: b -> (a -> b) -> Maybe a -> b #

The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

#### Examples

Basic usage:

>>> maybe False odd (Just 3)
True

>>> maybe False odd Nothing
False


Read 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 "")
0


Apply 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
""


fromMaybe :: a -> Maybe a -> a #

The fromMaybe function takes a default value and and Maybe value. If the Maybe is Nothing, it returns the default values; otherwise, it returns the value contained in the Maybe.

#### Examples

Basic usage:

>>> fromMaybe "" (Just "Hello, World!")
"Hello, World!"

>>> fromMaybe "" Nothing
""


Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:

>>> import Text.Read ( readMaybe )
>>> fromMaybe 0 (readMaybe "5")
5
>>> fromMaybe 0 (readMaybe "")
0