forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.nula
More file actions
38 lines (33 loc) · 925 Bytes
/
Copy pathoption.nula
File metadata and controls
38 lines (33 loc) · 925 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 — Option combinators.
// Import: `import stdlib::option`
/// The Option type: represents a value that may or may not be present.
pub type Option[T] = Some(T) | None
/// Unwrap an Option, returning the Some value.
/// Panics (via Test.assert) on None.
pub fn unwrap[T](opt: Option[T]) -> T {
match opt {
Some(x) => x,
None => perform Test.assert(false, "called `unwrap` on a None value"),
}
}
/// Returns true if the Option is Some.
pub fn is_some[T](opt: Option[T]) -> Bool {
match opt {
Some(_) => true,
None => false,
}
}
/// Returns true if the Option is None.
pub fn is_none[T](opt: Option[T]) -> Bool {
match opt {
Some(_) => false,
None => true,
}
}
/// Map a function over an Option.
pub fn map[T, U](opt: Option[T], f) -> Option[U] {
match opt {
Some(x) => Some(f(x)),
None => None,
}
}