File tree Expand file tree Collapse file tree 2 files changed +36
-11
lines changed
Expand file tree Collapse file tree 2 files changed +36
-11
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,7 @@ import Json.Decode.Extra
9191import Json.Decode.Pipeline
9292import Json.Encode exposing (Value )
9393import 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-
712703resultToDecoder : Result String a -> Decoder a
713704resultToDecoder res =
714705 case res of
Original file line number Diff line number Diff line change 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)
You can’t perform that action at this time.
0 commit comments