Skip to content

Commit 5bdda36

Browse files
committed
Use short-circuiting Result.Extra.combineMap
1 parent fc1ab7b commit 5bdda36

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/OpenApi/Types.elm

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import Json.Decode.Extra
9191
import Json.Decode.Pipeline
9292
import Json.Encode exposing (Value)
9393
import Json.Schema.Definitions
94+
import Result.Extra
9495

9596

9697

@@ -667,8 +668,7 @@ multipleTypesDecoder lst =
667668
otherList ->
668669
otherList
669670
|> List.sort
670-
|> List.map Json.Schema.Definitions.stringToType
671-
|> foldResults
671+
|> Result.Extra.combineMap Json.Schema.Definitions.stringToType
672672
|> Result.map Json.Schema.Definitions.UnionType
673673
|> resultToDecoder
674674

@@ -700,15 +700,6 @@ failIfEmpty l =
700700
Json.Decode.succeed l
701701

702702

703-
foldResults : List (Result x y) -> Result x (List y)
704-
foldResults results =
705-
results
706-
|> List.foldl
707-
(\t -> Result.andThen (\r -> t |> Result.map (\a -> a :: r)))
708-
(Ok [])
709-
|> Result.map List.reverse
710-
711-
712703
resultToDecoder : Result String a -> Decoder a
713704
resultToDecoder res =
714705
case res of

src/Result/Extra.elm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Result.Extra exposing (combineMap)
2+
3+
{-|
4+
5+
@docs combineMap
6+
7+
-}
8+
9+
10+
{-| Map a function producing results on a list
11+
and combine those into a single result (holding a list).
12+
Also known as `traverse` on lists.
13+
14+
combineMap f xs == combine (List.map f xs)
15+
16+
-}
17+
combineMap : (a -> Result x b) -> List a -> Result x (List b)
18+
combineMap f ls =
19+
combineMapHelp f ls []
20+
21+
22+
combineMapHelp : (a -> Result x b) -> List a -> List b -> Result x (List b)
23+
combineMapHelp f list acc =
24+
case list of
25+
head :: tail ->
26+
case f head of
27+
Ok a ->
28+
combineMapHelp f tail (a :: acc)
29+
30+
Err x ->
31+
Err x
32+
33+
[] ->
34+
Ok (List.reverse acc)

0 commit comments

Comments
 (0)