From 4f5956b135f67fd3421c02e41e8b4c60713af183 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Wed, 20 Oct 2021 11:13:59 +0200 Subject: [PATCH 1/2] Faster combine --- src/Result/Extra.elm | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Result/Extra.elm b/src/Result/Extra.elm index e3cc28b..e2114d3 100644 --- a/src/Result/Extra.elm +++ b/src/Result/Extra.elm @@ -143,8 +143,24 @@ mapBoth errFunc okFunc result = Also known as `sequence` on lists. -} combine : List (Result x a) -> Result x (List a) -combine = - List.foldr (Result.map2 (::)) (Ok []) +combine list = + combineHelp list [] + |> Result.map List.reverse + + +combineHelp : List (Result x a) -> List a -> Result x (List a) +combineHelp list acc = + case list of + head :: tail -> + case head of + Ok a -> + combineHelp tail (a :: acc) + + Err x -> + Err x + + [] -> + Ok acc {-| Map a function producing results on a list From a7ac9ee4aa88c88c65968304dc373b9428a3357b Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Thu, 21 Oct 2021 20:26:10 +0200 Subject: [PATCH 2/2] Inline the call to List.reverse --- src/Result/Extra.elm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Result/Extra.elm b/src/Result/Extra.elm index e2114d3..27a6112 100644 --- a/src/Result/Extra.elm +++ b/src/Result/Extra.elm @@ -145,7 +145,7 @@ Also known as `sequence` on lists. combine : List (Result x a) -> Result x (List a) combine list = combineHelp list [] - |> Result.map List.reverse + combineHelp : List (Result x a) -> List a -> Result x (List a) @@ -160,7 +160,7 @@ combineHelp list acc = Err x [] -> - Ok acc + Ok (List.reverse acc) {-| Map a function producing results on a list