Module DA.Optional¶
The Optional
type encapsulates an optional value. A value of type
Optional a
either contains a value of type a
(represented as Some a
),
or it is empty (represented as None
). Using Optional
is a good way
to deal with errors or exceptional cases without resorting to
drastic measures such as error.
The Optional type is also an action. It is a simple kind of error
action, where all errors are represented by None
. A richer
error action can be built using the Either
type.
Functions¶
- fromSome
: Optional a -> a
The
fromSome
function extracts the element out of aSome
and throws an error if its argument isNone
.
- fromSomeNote
- : Text -> Optional a -> a
- catOptionals
: [Optional a] -> [a]
The
catOptionals
function takes a list ofOptionals
and returns a list of all theSome
values.
- listToOptional
: [a] -> Optional a
The
listToOptional
function returnsNone
on an empty list orSome
a where a is the first element of the list.
- optionalToList
: Optional a -> [a]
The
optionalToList
function returns an empty list when givenNone
or a singleton list when not givenNone
.
- fromOptional
: a -> Optional a -> a
The
fromOptional
function takes a default value and aOptional
value. If theOptional
isNone
, it returns the default values otherwise, it returns the value contained in theOptional
.
- isSome
-
The
isSome
function returnsTrue
iff its argument is of the formSome _
.
- mapOptional
: (a -> Optional b) -> [a] -> [b]
The
mapOptional
function is a version ofmap
which can throw out elements. In particular, the functional argument returns something of typeOptional b
. If this isNone
, no element is added on to the result list. If it isSome b
, thenb
is included in the result list.
- whenSome
: Applicative m => Optional a -> (a -> m ()) -> m ()
Perform some operation on
Some
, given the field inside theSome
.