From 1489e38aaa076112fe580cd096e4c827304d2bd3 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 14 Oct 2020 16:05:24 +1100 Subject: Add result.all --- src/gleam/result.gleam | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src') diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index 88f933a..2ebf2ef 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -1,3 +1,5 @@ +import gleam/list + /// Result represents the result of something that may succeed or not. /// `Ok` means it was successful, `Error` means it was not successful. /// @@ -199,3 +201,17 @@ pub fn or(first: Result(a, e), second: Result(a, e)) -> Result(a, e) { Error(_) -> second } } + +/// Combine a list of results into a single result. +/// If all elements in the list are Ok then returns an Ok holding the list of values. +/// If any element is Error then returns the first error. +/// +/// ## Examples +/// > all([Ok(1), Ok(2)]) +/// Ok([1, 2]) +/// +/// > all([Ok(1), Error("e")]) +/// Error("e") +pub fn all(results: List(Result(a, e))) -> Result(List(a), e) { + list.try_map(results, fn(x) { x }) +} -- cgit v1.2.3