From 7af5bc63628bc5ccc45fe093810292c37b354ea4 Mon Sep 17 00:00:00 2001 From: Al Dee Date: Fri, 22 May 2020 20:13:11 +0100 Subject: Add unwrap function to option module --- src/gleam/option.gleam | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') 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 + } +} -- cgit v1.2.3