forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.nula
More file actions
38 lines (33 loc) · 957 Bytes
/
Copy pathresult.nula
File metadata and controls
38 lines (33 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Nulang standard library — Result combinators.
// Import: `import stdlib::result`
/// The Result type: represents either success (Ok) or failure (Error).
pub type Result[T, E] = Ok(T) | Error(E)
/// Unwrap a Result, returning the Ok value.
/// Panics (via Test.assert) on Error.
pub fn unwrap[T, E](r: Result[T, E]) -> T {
match r {
Ok(x) => x,
Error(_) => perform Test.assert(false, "called `unwrap` on an Error value"),
}
}
/// Map a function over a successful Result.
pub fn map[T, U, E](r: Result[T, E], f) -> Result[U, E] {
match r {
Ok(x) => Ok(f(x)),
Error(e) => Error(e),
}
}
/// Returns true if the Result is Ok.
pub fn is_ok[T, E](r: Result[T, E]) -> Bool {
match r {
Ok(_) => true,
Error(_) => false,
}
}
/// Returns true if the Result is Error.
pub fn is_err[T, E](r: Result[T, E]) -> Bool {
match r {
Ok(_) => false,
Error(_) => true,
}
}