Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- justIf :: Bool -> a -> Maybe a
- justWhen :: Monad m => Bool -> a -> m (Maybe a)
- fromMaybe :: a -> Maybe a -> a
- fromJust :: a -> Maybe a -> a
- fromJustM :: Applicative m => m a -> Maybe a -> m a
- unsafeFromJust :: Maybe a -> a
- unsafeFromJustM :: (Monad m, MonadFail m) => Maybe a -> m a
- withJust :: (Applicative m, Mempty out) => Maybe a -> (a -> m out) -> m out
- withJust_ :: Applicative m => Maybe a -> (a -> m out) -> m ()
- withJustM :: (Monad m, Mempty out) => m (Maybe a) -> (a -> m out) -> m out
- withJustM_ :: Monad m => m (Maybe a) -> (a -> m out) -> m ()
- whenJust :: (Applicative m, Mempty out) => Maybe a -> m out -> m out
- whenJust_ :: Applicative m => Maybe a -> m out -> m ()
- whenJustM :: (Monad m, Mempty out) => m (Maybe a) -> m out -> m out
- whenJustM_ :: Monad m => m (Maybe a) -> m out -> m ()
- whenNothing :: (Applicative m, Mempty out) => Maybe a -> m out -> m out
- whenNothing_ :: Applicative m => Maybe a -> m out -> m ()
- whenNothingM :: (Monad m, Mempty out) => m (Maybe a) -> m out -> m out
- whenNothingM_ :: Monad m => m (Maybe a) -> m out -> m ()
- data Maybe a
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- catMaybes :: [Maybe a] -> [a]
- isNothing :: Maybe a -> Bool
- isJust :: Maybe a -> Bool
- maybe :: b -> (a -> b) -> Maybe a -> b
- isNothingT :: Monad m => MaybeT m a -> m Bool
- isJustT :: Monad m => MaybeT m a -> m Bool
- nothing :: Monad m => MaybeT m a
- just :: Monad m => a -> MaybeT m a
- maybeT :: Monad m => m b -> (a -> m b) -> MaybeT m a -> m b
- mapMaybeT :: (m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
- newtype MaybeT (m :: Type -> Type) a = MaybeT {}
- exceptToMaybeT :: Functor m => ExceptT e m a -> MaybeT m a
- maybeToExceptT :: Functor m => e -> MaybeT m a -> ExceptT e m a
Documentation
fromJustM :: Applicative m => m a -> Maybe a -> m a Source #
unsafeFromJust :: Maybe a -> a Source #
Warning: Do not use in production code
withJust_ :: Applicative m => Maybe a -> (a -> m out) -> m () Source #
withJustM_ :: Monad m => m (Maybe a) -> (a -> m out) -> m () Source #
whenJust_ :: Applicative m => Maybe a -> m out -> m () Source #
whenJustM_ :: Monad m => m (Maybe a) -> m out -> m () Source #
whenNothing :: (Applicative m, Mempty out) => Maybe a -> m out -> m out Source #
whenNothing_ :: Applicative m => Maybe a -> m out -> m () Source #
whenNothingM_ :: Monad m => m (Maybe a) -> m out -> m () Source #
The Maybe
type encapsulates an optional value. A value of type
either contains a value of type Maybe
aa
(represented as
),
or it is empty (represented as Just
aNothing
). 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.
Instances
Monad Maybe | Since: base-2.1 |
Functor Maybe | Since: base-2.1 |
MonadFix Maybe | Since: base-2.1 |
Defined in Control.Monad.Fix | |
MonadFail Maybe | Since: base-4.9.0.0 |
Defined in Control.Monad.Fail | |
Applicative Maybe | Since: base-2.1 |
Foldable Maybe | Since: base-2.1 |
Defined in Data.Foldable fold :: 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 # elem :: Eq a => a -> Maybe a -> Bool # maximum :: Ord a => Maybe a -> a # minimum :: Ord a => Maybe a -> a # | |
Traversable Maybe | Since: base-2.1 |
Alternative Maybe | Since: base-2.1 |
MonadPlus Maybe | Since: base-2.1 |
Eq1 Maybe | Since: base-4.9.0.0 |
Ord1 Maybe | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Read1 Maybe | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Show1 Maybe | Since: base-4.9.0.0 |
NFData1 Maybe | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
MonadThrow Maybe | |
Defined in Control.Monad.Catch | |
Hashable1 Maybe | |
Defined in Data.Hashable.Class | |
Apply Maybe | |
Pointed Maybe | |
Defined in Data.Pointed | |
Alt Maybe | |
Bind Maybe | |
FunctorWithIndex () Maybe | |
Defined in Control.Lens.Indexed | |
FoldableWithIndex () Maybe | |
TraversableWithIndex () Maybe | |
Defined in Control.Lens.Indexed itraverse :: Applicative f => (() -> a -> f b) -> Maybe a -> f (Maybe b) # itraversed :: IndexedTraversal () (Maybe a) (Maybe b) a b # | |
Eq a => Eq (Maybe a) | Since: base-2.1 |
Data a => Data (Maybe a) | Since: base-4.0.0.0 |
Defined in Data.Data gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Maybe a -> c (Maybe a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Maybe a) # toConstr :: Maybe a -> Constr # dataTypeOf :: Maybe a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Maybe a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Maybe a)) # gmapT :: (forall b. Data b => b -> b) -> Maybe a -> Maybe a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Maybe a -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Maybe a -> r # gmapQ :: (forall d. Data d => d -> u) -> Maybe a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Maybe a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Maybe a -> m (Maybe a) # | |
Ord a => Ord (Maybe a) | Since: base-2.1 |
Read a => Read (Maybe a) | Since: base-2.1 |
Show a => Show (Maybe a) | Since: base-2.1 |
Generic (Maybe a) | |
Semigroup a => Semigroup (Maybe a) | Since: base-4.9.0.0 |
Semigroup a => Monoid (Maybe a) | Lift a semigroup into Since 4.11.0: constraint on inner Since: base-2.1 |
Lift a => Lift (Maybe a) | |
Hashable a => Hashable (Maybe a) | |
Defined in Data.Hashable.Class | |
SingKind a => SingKind (Maybe a) | Since: base-4.9.0.0 |
Default (Maybe a) | |
Defined in Data.Default.Class | |
NFData a => NFData (Maybe a) | |
Defined in Control.DeepSeq | |
Ixed (Maybe a) | |
Defined in Control.Lens.At | |
At (Maybe a) | |
AsEmpty (Maybe a) | |
Defined in Control.Lens.Empty | |
Mempty (Maybe a) | |
Defined in Data.Monoids | |
Recursive (Maybe a) | |
Defined in Data.Functor.Foldable project :: Maybe a -> Base (Maybe a) (Maybe a) # cata :: (Base (Maybe a) a0 -> a0) -> Maybe a -> a0 # para :: (Base (Maybe a) (Maybe a, a0) -> a0) -> Maybe a -> a0 # gpara :: (Corecursive (Maybe a), Comonad w) => (forall b. Base (Maybe a) (w b) -> w (Base (Maybe a) b)) -> (Base (Maybe a) (EnvT (Maybe a) w a0) -> a0) -> Maybe a -> a0 # prepro :: Corecursive (Maybe a) => (forall b. Base (Maybe a) b -> Base (Maybe a) b) -> (Base (Maybe a) a0 -> a0) -> Maybe a -> a0 # gprepro :: (Corecursive (Maybe a), Comonad w) => (forall b. Base (Maybe a) (w b) -> w (Base (Maybe a) b)) -> (forall c. Base (Maybe a) c -> Base (Maybe a) c) -> (Base (Maybe a) (w a0) -> a0) -> Maybe a -> a0 # | |
Corecursive (Maybe a) | |
Defined in Data.Functor.Foldable embed :: Base (Maybe a) (Maybe a) -> Maybe a # ana :: (a0 -> Base (Maybe a) a0) -> a0 -> Maybe a # apo :: (a0 -> Base (Maybe a) (Either (Maybe a) a0)) -> a0 -> Maybe a # postpro :: Recursive (Maybe a) => (forall b. Base (Maybe a) b -> Base (Maybe a) b) -> (a0 -> Base (Maybe a) a0) -> a0 -> Maybe a # gpostpro :: (Recursive (Maybe a), Monad m) => (forall b. m (Base (Maybe a) b) -> Base (Maybe a) (m b)) -> (forall c. Base (Maybe a) c -> Base (Maybe a) c) -> (a0 -> Base (Maybe a) (m a0)) -> a0 -> Maybe a # | |
Generic1 Maybe | |
SingI (Nothing :: Maybe a) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
Each (Maybe a) (Maybe b) a b |
|
SingI a2 => SingI (Just a2 :: Maybe a1) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
type Rep (Maybe a) | Since: base-4.6.0.0 |
data Sing (b :: Maybe a) | |
type DemoteRep (Maybe a) | |
Defined in GHC.Generics | |
type Index (Maybe a) | |
Defined in Control.Lens.At | |
type IxValue (Maybe a) | |
Defined in Control.Lens.At | |
type Base (Maybe a) | Example boring stub for non-recursive data types |
type Rep1 Maybe | Since: base-4.6.0.0 |
mapMaybe :: (a -> Maybe b) -> [a] -> [b] #
The mapMaybe
function is a version of map
which can throw
out elements. In particular, the functional argument returns
something of type
. If this is Maybe
bNothing
, no element
is added on to the result list. If it is
, then Just
bb
is
included in the result list.
Examples
Using
is a shortcut for mapMaybe
f x
in most cases:catMaybes
$ map
f x
>>>
import Text.Read ( readMaybe )
>>>
let readMaybeInt = readMaybe :: String -> Maybe Int
>>>
mapMaybe readMaybeInt ["1", "Foo", "3"]
[1,3]>>>
catMaybes $ map readMaybeInt ["1", "Foo", "3"]
[1,3]
If we map the Just
constructor, the entire list should be returned:
>>>
mapMaybe Just [1,2,3]
[1,2,3]
catMaybes :: [Maybe a] -> [a] #
The catMaybes
function takes a list of Maybe
s and returns
a list of all the Just
values.
Examples
Basic usage:
>>>
catMaybes [Just 1, Nothing, Just 3]
[1,3]
When constructing a list of Maybe
values, catMaybes
can be used
to return all of the "success" results (if the list is the result
of a map
, then mapMaybe
would be more appropriate):
>>>
import Text.Read ( readMaybe )
>>>
[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[Just 1,Nothing,Just 3]>>>
catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[1,3]
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
""
newtype MaybeT (m :: Type -> Type) a #
The parameterizable maybe monad, obtained by composing an arbitrary
monad with the Maybe
monad.
Computations are actions that may produce a value or exit.
The return
function yields a computation that produces that
value, while >>=
sequences two subcomputations, exiting if either
computation does.
Instances
exceptToMaybeT :: Functor m => ExceptT e m a -> MaybeT m a #