aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-02-22 17:44:27 +0000
committerLouis Pilfold <louis@lpil.uk>2022-02-22 17:44:27 +0000
commitab21b63529e9d81d3855207b4cd7be0631278acb (patch)
tree88e08ee98b39fbaf860a0985e737bf7f81a58cdf /src
parentabb7b73fddb3d2ebbd1b1ba30d40459704f6fdab (diff)
downloadgleam_stdlib-ab21b63529e9d81d3855207b4cd7be0631278acb.tar.gz
gleam_stdlib-ab21b63529e9d81d3855207b4cd7be0631278acb.zip
result.unwrap_error
Diffstat (limited to 'src')
-rw-r--r--src/gleam/result.gleam46
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