std/option
Optional values: Some(value) when there is a value, and None when
there is not.
In std, a function that may find nothing gives an Option, for
example list.first and dict.get. A function that may fail, such as
int.parse, gives a Result.
Option
A value that may be there (Some) or not (None).
Option(A) = Some(A) | None is_some
True when the option is Some.
is_some = <A>(option : Option(A)) -> Bool { is_none
True when the option is None.
is_none = <A>(option : Option(A)) -> Bool { map
Changes the value in a Some with a function. None stays None.
option.map(Some(1), (x) { x + 1 }) // Some(2) map = <A, B, N>(over : Option(A), with : (A) <- N -> B) -> Option(B) { then
Calls a function that gives an option on the value in a Some. None
stays None. This puts together steps that may each find nothing.
option.then(Some(1), (x) { Some(x + 1) }) // Some(2) then = <A, B, N>(option : Option(A), apply : (A) <- N -> Option(B)) -> Option( flatten
Takes away one level of Option.
flatten = <A>(option : Option(Option(A))) -> Option(A) { unwrap
The value in a Some, or default for None.
unwrap = <A>(option : Option(A), or : A) -> A { lazy_unwrap
The value in a Some, or the value of default() for None.
lazy_unwrap = <A, N>(option : Option(A), or : () <- N -> A) -> A { or
The first option when it is Some, and the second when not.
or = <A>(first : Option(A), second : Option(A)) -> Option(A) { lazy_or
The first option when it is Some, and the value of second() when not.
lazy_or = <A, N>(first : Option(A), second : () <- N -> Option(A)) -> Option( to_result
Ok with the value of a Some, or Error(error) for None.
to_result = <A, E>(option : Option(A), error : E) -> Result(A, E) { from_result
Some with the value of an Ok, or None for an Error.
from_result = <A, E>(result : Result(A, E)) -> Option(A) { all
Some with all the values when every option is Some, and None when
one of them is None.
option.all([Some(1), Some(2)]) // Some([1, 2])
option.all([Some(1), None]) // None all = <A>(options : List(Option(A))) -> Option(List(A)) { values
The values of the Some options, in order.
option.values([Some(1), None, Some(3)]) // [1, 3] values = <A>(options : List(Option(A))) -> List(A) { filter
Keeps a Some only when its value passes the test.
filter = <A, N>(option : Option(A), keeping : (A) <- N -> Bool) -> Option(A) { map2
Puts the values of two options together when both are Some.
map2 = <A, B, C, N>(