diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/result.gleam | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index c774219..5d77408 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -141,11 +141,15 @@ pub fn then( /// /// ## Examples /// -/// > unwrap(Ok(1), 0) -/// 1 +/// ```gleam +/// > unwrap(Ok(1), 0) +/// 1 +/// ``` /// -/// > unwrap(Error(""), 0) -/// 0 +/// ```gleam +/// > unwrap(Error(""), 0) +/// 0 +/// ``` /// pub fn unwrap(result: Result(a, e), or default: a) -> a { case result { @@ -159,11 +163,15 @@ pub fn unwrap(result: Result(a, e), or default: a) -> a { /// /// ## Examples /// -/// > lazy_unwrap(Ok(1), fn() { 0 }) -/// 1 +/// ```gleam +/// > lazy_unwrap(Ok(1), fn() { 0 }) +/// 1 +/// ``` /// -/// > lazy_unwrap(Error(""), fn() { 0 }) -/// 0 +/// ```gleam +/// > lazy_unwrap(Error(""), fn() { 0 }) +/// 0 +/// ``` /// pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a { case result { @@ -172,6 +180,28 @@ pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a { } } +/// Extracts the `Error` value from a result, returning a default value if the result +/// is an `Ok`. +/// +/// ## Examples +/// +/// ```gleam +/// > unwrap_error(Error(1), 0) +/// 1 +/// ``` +/// +/// ```gleam +/// > unwrap_error(Ok(""), 0) +/// 0 +/// ``` +/// +pub fn unwrap_error(result: Result(a, e), or default: e) -> e { + case result { + Ok(_) -> default + Error(e) -> e + } +} + /// Transforms any error into `Error(Nil)`. /// /// ## Examples |