Optional type encapsulates an optional value. A value of type
Optional a either contains a value of type
a (represented as
or it is empty (represented as
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
: Optional a -> a
fromSomefunction extracts the element out of a
Someand throws an error if its argument is
Note that in most cases you should prefer using
fromSomeNoteto get a better error on failures.
fromSomebut with a custom error message.
: [Optional a] -> [a]
catOptionalsfunction takes a list of
Optionalsand returns a list of all the
: [a] -> Optional a
Noneon an empty list or
Somea where a is the first element of the list.
: Optional a -> [a]
optionalToListfunction returns an empty list when given
Noneor a singleton list when not given
: a -> Optional a -> a
fromOptionalfunction takes a default value and a
Optionalvalue. If the
None, it returns the default values otherwise, it returns the value contained in the
Trueiff its argument is of the form
: (a -> Optional b) -> [a] -> [b]
mapOptionalfunction is a version of
mapwhich can throw out elements. In particular, the functional argument returns something of type
Optional b. If this is
None, no element is added on to the result list. If it is
Some b, then
bis included in the result list.
: Applicative m => Optional a -> (a -> m ()) -> m ()
Perform some operation on
Some, given the field inside the
: (a -> Optional b) -> [a] -> Optional b
findOptionalreturns the value of the predicate at the first element where it returns
findOptionalis similar to
findbut it allows you to return a value from the predicate. This is useful both as a more type safe version if the predicate corresponds to a pattern match and for performance to avoid duplicating work performed in the predicate.