Reputation: 77
I have this type alias:
type alias ResponseLine =
{ tag : Maybe String
, vr : Maybe String
, value : Maybe String
, comments : Maybe String
, len : Maybe String
, vm : Maybe String
, name : Maybe String
}
I Get a lot of these List Maybe String 's submatches back from regexing a Http.request text response:
[ Just "0008,0005"
, Just "CS"
, Just "ISO_IR100"
, Nothing
, Just "10"
, Just "1"
, Just "SpecificCharacterSet"
]
I need a way to go from those Lists to those type aliases. I tried:
getResponseLine : List Maybe String -> ResponseLine
getResponseLine =
foldl (\arg fn -> fn arg) ResponseLine
While there are no errors shown at all in VSCode, reactor cannot build my app, after trying for some time it says:
elm-make: out of memory
I guess some recursive stuff is going on. So, do i need to use Array.fromList and use indexes, or is there a more functional approach i have overlooked? The internet is too full with posts of problems the other way around :( Thanks in advance.
Upvotes: 1
Views: 763
Reputation: 64959
If you want to convert a List
of 7 Maybe String
values to a ResponseLine
object, then something like the following should work:
getResponseLine: List (Maybe String) -> ResponseLine
getResponseLine strings =
case strings of
[ tag, vr, value, comments, len, vm, name ] ->
ResponseLine tag vr value comments len vm name
_ ->
Debug.crash "TODO handle errors if list doesn't have 7 items"
I'll leave it up to you to decide what error-handling your need if the list of Maybe String
values doesn't have length 7. Note also that I've declared the argument to getResponseLine
as of type List (Maybe String)
rather than List Maybe String
: the compiler will interpret the latter as List
being a type that takes two type variables with the values of the type variables being Maybe
and String
.
I suspect the out-of-memory error is caused while the Elm compiler is trying to figure out types in your call to foldl
. It looks related to this Elm compiler bug.
Upvotes: 3