diff options
author | Al Dee <amyalicedee@gmail.com> | 2020-05-22 20:13:11 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-05-26 19:19:29 +0100 |
commit | 7af5bc63628bc5ccc45fe093810292c37b354ea4 (patch) | |
tree | 4a77cf45955bddb81c260695185c94994c459033 /src | |
parent | 74b8029a37bc1d5470a19a6effa67763dcc32994 (diff) | |
download | gleam_stdlib-7af5bc63628bc5ccc45fe093810292c37b354ea4.tar.gz gleam_stdlib-7af5bc63628bc5ccc45fe093810292c37b354ea4.zip |
Add unwrap function to option module
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/option.gleam | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam index e5be4cc..663cd1f 100644 --- a/src/gleam/option.gleam +++ b/src/gleam/option.gleam @@ -68,3 +68,20 @@ pub fn from_result(result: Result(a, e)) -> Option(a) { _ -> None } } + +/// Extract the value from an option, returning a default value if there is none. +/// +/// ## Examples +/// +/// > unwrap(Some(1), 0) +/// 1 +/// +/// > unwrap(None, 0) +/// 0 +/// +pub fn unwrap(option: Option(a), or default: a) -> a { + case option { + Some(x) -> x + None -> default + } +} |