Data.Maybe
Operations over Option.
The type is wired in; Base includes this module, so these are in unqualified scope everywhere.
Functions and Values
is_some
is_some : forall a. (Option(a)) -> Bool
True when the option holds a value.
is_some(Some(5))
true
is_none
is_none : forall a. (Option(a)) -> Bool
True when the option is empty.
is_none(None)
true
unwrap_or
unwrap_or : forall a. (a, Option(a)) -> a
The contained value, or the default d when the option is empty.
(unwrap_or(0, Some(5)), unwrap_or(0, None))
(5, 0)
The default must match the contained type, and the second argument must be an option:
unwrap_or(0, 5)
map_option
map_option : forall e0 a b. ((b) -> a ! {e0}, Option(b)) -> Option(a) ! {e0}
Apply f to the contained value, leaving None untouched.
map_option(\(x) -> x * 2, Some(21))
Some(42)
and_then
and_then : forall e0 a b. ((a) -> Option(b) ! {e0}, Option(a)) -> Option(b) ! {e0}
Chain an option-returning function, short-circuiting on None (monadic bind for Option).
and_then(\(x) -> if x > 0 then Some(x * 10) else None, Some(4))
Some(40)
map_or
map_or : forall e0 a b. (a, (b) -> a ! {e0}, Option(b)) -> a ! {e0}
Apply f to the contained value, or return the default d when empty: map_option and unwrap_or in one step.
map_or(0, \(x) -> x + 1, Some(41))
42
option_or
option_or : forall a. (Option(a), Option(a)) -> Option(a)
The option itself when it holds a value, otherwise the alternative alt.
option_or(Some(1), None)
Some(1)
option_to_list
option_to_list : forall a. (Option(a)) -> List(a)
An empty or one-element list from an option.
option_to_list(Some(7))
[7]
both
both : forall a b. (Option(a), Option(b)) -> Option((a, b))
Pair two options: Some only when both hold values.
(both(Some(1), Some(2)), both(Some(1), None))
(Some((1, 2)), None)
option_fold_r
option_fold_r : forall e0 a b. ((a, b) -> b ! {e0}, b, Option(a)) -> b ! {e0}
Fold an option right-to-left: g(x, z) on Some(x), z on None.
option_fold_r(\(x, z) -> x + z, 10, Some(5))
15
option_fold_l
option_fold_l : forall e0 a b. ((a, b) -> a ! {e0}, a, Option(b)) -> a ! {e0}
Fold an option left-to-right: g(z, x) on Some(x), z on None.
option_fold_l(\(z, x) -> z + x, 10, Some(5))
15
option_bind
option_bind : forall e0 a b. (Option(a), (a) -> Option(b) ! {e0}) -> Option(b) ! {e0}
and_then with the option first: reads as a pipeline of fallible steps.
option_bind(Some(4), \(x) -> Some(x * 10))
Some(40)
option_ap
option_ap : forall e0 a b. (Option((b) -> a ! {e0}), Option(b)) -> Option(a) ! {e0}
Apply an optional function to an optional value (applicative apply).
option_ap(Some(\(x) -> x + 1), Some(41))
Some(42)