tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

136 lignes · 3964 octetsBrut · Historique
1/// Results of fallible computations (`Result` is in the prelude).
2///
3/// A function that can fail for a reason the caller should handle
4/// returns `Result<a, e>`: `Ok(value)` or `Error(reason)` (ADR 0024).
5/// Match on it, or chain steps with `use`:
6///
7/// fn read_port(text: String) -> Result<Int, String> {
8/// use n <- result.try(string.to_int(text))
9/// if n < 65536 { Ok(n) } else { Error("port out of range: " <> show(n)) }
10/// }
11
12/// Whether `r` is `Ok`.
13pub fn is_ok :: r: Result<a, x> -> Bool
14{
15 match r { Ok(_) -> True, Error(_) -> False }
16}
17
18/// Whether `r` is an `Error`.
19pub fn is_error :: r: Result<a, x> -> Bool
20{
21 match r { Ok(_) -> False, Error(_) -> True }
22}
23
24/// Apply `f` to the value of an `Ok`; an `Error` stays as it is.
25pub fn map :: r: Result<a, x> f: fn(a) -> b / <e> -> Result<b, x> / <e>
26{
27 match r { Ok(v) -> Ok(f v), Error(err) -> Error(err) }
28}
29
30/// Apply `f` to the reason of an `Error`; an `Ok` stays as it is. For
31/// example, to wrap a library's error in the caller's error type.
32pub fn map_error :: r: Result<a, x> f: fn(x) -> y / <e> -> Result<a, y> / <e>
33{
34 match r { Ok(v) -> Ok(v), Error(err) -> Error(f err) }
35}
36
37/// Replace the reason of an `Error` with `reason`.
38pub fn replace_error :: r: Result<a, x> reason: y -> Result<a, y>
39{
40 match r { Ok(v) -> Ok(v), Error(_) -> Error(reason) }
41}
42
43/// Continue with `next` on success; for `use`:
44/// `use v <- result.try(parse(input))`.
45pub fn try ::
46 r: Result<a, x>
47 next: fn(a) -> Result<b, x> / <e>
48 -> Result<b, x> / <e>
49{
50 match r { Ok(v) -> next v, Error(err) -> Error(err) }
51}
52
53/// The inner result of an `Ok`, or the outer `Error`.
54pub fn flatten :: r: Result<Result<a, x>, x> -> Result<a, x>
55{
56 match r { Ok(inner) -> inner, Error(err) -> Error(err) }
57}
58
59/// The value of an `Ok`, or `default`.
60pub fn unwrap_or :: r: Result<a, x> default: a -> a
61{
62 match r { Ok(v) -> v, Error(_) -> default }
63}
64
65/// The value of an `Ok`, or `f` applied to the reason of the `Error`
66/// (called only then).
67pub fn unwrap_or_else :: r: Result<a, x> f: fn(x) -> a / <e> -> a / <e>
68{
69 match r { Ok(v) -> v, Error(err) -> f err }
70}
71
72/// The value of an `Ok`. On an `Error`, panic with `message` and the
73/// reason: only for results that cannot be errors unless the program has
74/// a bug (panics are for bugs, ADR 0024).
75pub fn expect :: r: Result<a, x> message: String -> a where Debug<x>
76{
77 match r { Ok(v) -> v, Error(err) -> panic (message <> ": " <> debug err) }
78}
79
80/// `Some` value of an `Ok`, or `None`, dropping the reason.
81pub fn to_option :: r: Result<a, x> -> Option<a>
82{
83 match r { Ok(v) -> Some(v), Error(_) -> None }
84}
85
86/// The values of all the results, in order, or the first `Error`.
87pub fn all :: results: List<Result<a, x>> -> Result<List<a>, x>
88{
89 match results
90 { [] -> Ok([])
91 , [Error(err), ..] -> Error(err)
92 , [Ok(v), ..rest] -> match all rest
93 { Ok(vs) -> Ok([v, ..vs]), Error(err) -> Error(err) }
94 }
95}
96
97/// The values of the `Ok`s, in order; the `Error`s are dropped.
98pub fn values :: results: List<Result<a, x>> -> List<a>
99{
100 match results
101 { [] -> []
102 , [Ok(v), ..rest] -> [v, ..values rest]
103 , [Error(_), ..rest] -> values rest
104 }
105}
106
107/// `r` if it is `Ok`, else `other`: the first success, or the last
108/// error (Haskell's `<|>` for `Either`).
109pub fn or :: r: Result<a, x> other: Result<a, x> -> Result<a, x>
110{
111 match r { Ok(v) -> Ok(v), Error(_) -> other }
112}
113
114/// `r` if it is `Ok`, else the result of `other` (called only then).
115pub fn lazy_or ::
116 r: Result<a, x>
117 other: fn() -> Result<a, x> / <e>
118 -> Result<a, x> / <e>
119{
120 match r { Ok(v) -> Ok(v), Error(_) -> other () }
121}
122
123/// `f` applied to both values if both are `Ok`, else the first `Error`
124/// (from `a`, then from `b`).
125pub fn map2 ::
126 a: Result<a, x>
127 b: Result<b, x>
128 f: fn(a, b) -> c / <e>
129 -> Result<c, x> / <e>
130{
131 match (a, b)
132 { (Ok(v), Ok(w)) -> Ok(f v w)
133 , (Error(err), _) -> Error(err)
134 , (_, Error(err)) -> Error(err)
135 }
136}