/// Results of fallible computations (`Result` is in the prelude). /// /// A function that can fail for a reason the caller should handle /// returns `Result`: `Ok(value)` or `Error(reason)` (ADR 0024). /// Match on it, or chain steps with `use`: /// /// fn read_port(text: String) -> Result { /// use n <- result.try(string.to_int(text)) /// if n < 65536 { Ok(n) } else { Error("port out of range: " <> show(n)) } /// } /// Whether `r` is `Ok`. pub fn is_ok :: r: Result -> Bool { match r { Ok(_) -> True, Error(_) -> False } } /// Whether `r` is an `Error`. pub fn is_error :: r: Result -> Bool { match r { Ok(_) -> False, Error(_) -> True } } /// Apply `f` to the value of an `Ok`; an `Error` stays as it is. pub fn map :: r: Result f: fn(a) -> b / -> Result / { match r { Ok(v) -> Ok(f v), Error(err) -> Error(err) } } /// Apply `f` to the reason of an `Error`; an `Ok` stays as it is. For /// example, to wrap a library's error in the caller's error type. pub fn map_error :: r: Result f: fn(x) -> y / -> Result / { match r { Ok(v) -> Ok(v), Error(err) -> Error(f err) } } /// Replace the reason of an `Error` with `reason`. pub fn replace_error :: r: Result reason: y -> Result { match r { Ok(v) -> Ok(v), Error(_) -> Error(reason) } } /// Continue with `next` on success; for `use`: /// `use v <- result.try(parse(input))`. pub fn try :: r: Result next: fn(a) -> Result / -> Result / { match r { Ok(v) -> next v, Error(err) -> Error(err) } } /// The inner result of an `Ok`, or the outer `Error`. pub fn flatten :: r: Result, x> -> Result { match r { Ok(inner) -> inner, Error(err) -> Error(err) } } /// The value of an `Ok`, or `default`. pub fn unwrap_or :: r: Result default: a -> a { match r { Ok(v) -> v, Error(_) -> default } } /// The value of an `Ok`, or `f` applied to the reason of the `Error` /// (called only then). pub fn unwrap_or_else :: r: Result f: fn(x) -> a / -> a / { match r { Ok(v) -> v, Error(err) -> f err } } /// The value of an `Ok`. On an `Error`, panic with `message` and the /// reason: only for results that cannot be errors unless the program has /// a bug (panics are for bugs, ADR 0024). pub fn expect :: r: Result message: String -> a where Debug { match r { Ok(v) -> v, Error(err) -> panic (message <> ": " <> debug err) } } /// `Some` value of an `Ok`, or `None`, dropping the reason. pub fn to_option :: r: Result -> Option { match r { Ok(v) -> Some(v), Error(_) -> None } } /// The values of all the results, in order, or the first `Error`. pub fn all :: results: List> -> Result, x> { match results { [] -> Ok([]) , [Error(err), ..] -> Error(err) , [Ok(v), ..rest] -> match all rest { Ok(vs) -> Ok([v, ..vs]), Error(err) -> Error(err) } } } /// The values of the `Ok`s, in order; the `Error`s are dropped. pub fn values :: results: List> -> List { match results { [] -> [] , [Ok(v), ..rest] -> [v, ..values rest] , [Error(_), ..rest] -> values rest } } /// `r` if it is `Ok`, else `other`: the first success, or the last /// error (Haskell's `<|>` for `Either`). pub fn or :: r: Result other: Result -> Result { match r { Ok(v) -> Ok(v), Error(_) -> other } } /// `r` if it is `Ok`, else the result of `other` (called only then). pub fn lazy_or :: r: Result other: fn() -> Result / -> Result / { match r { Ok(v) -> Ok(v), Error(_) -> other () } } /// `f` applied to both values if both are `Ok`, else the first `Error` /// (from `a`, then from `b`). pub fn map2 :: a: Result b: Result f: fn(a, b) -> c / -> Result / { match (a, b) { (Ok(v), Ok(w)) -> Ok(f v w) , (Error(err), _) -> Error(err) , (_, Error(err)) -> Error(err) } }