From 7b67ae1502b10edbb1589b831db1d8d856dc9b88 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Tue, 22 Feb 2022 17:47:02 +0000 Subject: result.unwrap_both --- src/gleam/result.gleam | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index 5d77408..9f419b5 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -202,6 +202,28 @@ pub fn unwrap_error(result: Result(a, e), or default: e) -> e { } } +/// Extracts the inner value from a result. Both the value and error must be of +/// the same type. +/// +/// ## Examples +/// +/// ```gleam +/// > unwrap_both(Error(1)) +/// 1 +/// ``` +/// +/// ```gleam +/// > unwrap_both(Ok(2)) +/// 2 +/// ``` +/// +pub fn unwrap_both(result: Result(a, a)) -> a { + case result { + Ok(a) -> a + Error(a) -> a + } +} + /// Transforms any error into `Error(Nil)`. /// /// ## Examples -- cgit v1.2.3