/// Optional values (`Option` is in the prelude). /// /// `Option` is for a value that may be absent, where absence is not a /// failure (a key not in a dictionary, a variable not set). A failure with /// a reason is a `Result` (ADR 0024); `to_result` turns one into the other. /// Whether `o` is `Some`. pub fn is_some :: o: Option -> Bool { match o { Some(_) -> True, None -> False } } /// Whether `o` is `None`. pub fn is_none :: o: Option -> Bool { match o { Some(_) -> False, None -> True } } /// Apply `f` to the value of a `Some`. pub fn map :: o: Option f: fn(a) -> b / -> Option / { match o { Some(x) -> Some(f x), None -> None } } /// Continue with `next` on `Some`; for `use`: /// `use user <- option.then(find_user(name))`. pub fn then :: o: Option next: fn(a) -> Option / -> Option / { match o { Some(x) -> next x, None -> None } } /// The value of a `Some`, or `default`. pub fn unwrap_or :: o: Option default: a -> a { match o { Some(x) -> x, None -> default } } /// `Ok` with the value of a `Some`, or `Error(reason)`: absence becomes /// a failure with a reason. pub fn to_result :: o: Option reason: x -> Result { match o { Some(x) -> Ok(x), None -> Error(reason) } } /// The values of the `Some`s, in order. pub fn values :: options: List> -> List { match options { [] -> [] , [Some(x), ..rest] -> [x, ..values rest] , [None, ..rest] -> values rest } } /// `o` if it is `Some`, else `other` (Haskell's `<|>` for `Maybe`). pub fn or :: o: Option other: Option -> Option { match o { Some(x) -> Some(x), None -> other } } /// `o` if it is `Some`, else the result of `other` (called only then). pub fn lazy_or :: o: Option other: fn() -> Option / -> Option / { match o { Some(x) -> Some(x), None -> other () } } /// `f` applied to both values if both are `Some`, else `None`. pub fn map2 :: a: Option b: Option f: fn(a, b) -> c / -> Option / { match (a, b) { (Some(x), Some(y)) -> Some(f x y), _ -> None } }