如何有效地将列表转换为 Elm 中的类型别名
how to effectively convert a list to type alias in Elm
我有这个类型的别名:
type alias ResponseLine =
{ tag : Maybe String
, vr : Maybe String
, value : Maybe String
, comments : Maybe String
, len : Maybe String
, vm : Maybe String
, name : Maybe String
}
我从 Http.request 文本响应的正则表达式中得到了很多这些 List Maybe String 的子匹配:
[ Just "0008,0005"
, Just "CS"
, Just "ISO_IR100"
, Nothing
, Just "10"
, Just "1"
, Just "SpecificCharacterSet"
]
我需要一种从这些列表到那些类型别名的方法。我试过了:
getResponseLine : List Maybe String -> ResponseLine
getResponseLine =
foldl (\arg fn -> fn arg) ResponseLine
虽然 VSCode 中完全没有显示任何错误,但反应器无法构建我的应用程序,在尝试了一段时间后它说:
elm-make: out of memory
我想一些递归的事情正在发生。那么,我是否需要使用 Array.fromList 并使用索引,还是我忽略了一种更实用的方法?
互联网上到处都是问题的帖子:(
提前致谢。
如果你想将 7 个 Maybe String
值的 List
转换为 ResponseLine
对象,那么类似下面的东西应该可以工作:
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"
如果 Maybe String
值列表的长度不是 7,我将由您来决定您需要什么错误处理。另请注意,我已将参数声明为 getResponseLine
作为类型 List (Maybe String)
而不是 List Maybe String
:编译器会将后者解释为 List
是一种采用两个类型变量的类型,类型变量的值为 Maybe
和 String
。
我怀疑内存不足错误是在 Elm 编译器试图找出您对 foldl
的调用中的类型时引起的。它看起来与 this Elm compiler bug.
有关
我有这个类型的别名:
type alias ResponseLine =
{ tag : Maybe String
, vr : Maybe String
, value : Maybe String
, comments : Maybe String
, len : Maybe String
, vm : Maybe String
, name : Maybe String
}
我从 Http.request 文本响应的正则表达式中得到了很多这些 List Maybe String 的子匹配:
[ Just "0008,0005"
, Just "CS"
, Just "ISO_IR100"
, Nothing
, Just "10"
, Just "1"
, Just "SpecificCharacterSet"
]
我需要一种从这些列表到那些类型别名的方法。我试过了:
getResponseLine : List Maybe String -> ResponseLine
getResponseLine =
foldl (\arg fn -> fn arg) ResponseLine
虽然 VSCode 中完全没有显示任何错误,但反应器无法构建我的应用程序,在尝试了一段时间后它说:
elm-make: out of memory
我想一些递归的事情正在发生。那么,我是否需要使用 Array.fromList 并使用索引,还是我忽略了一种更实用的方法? 互联网上到处都是问题的帖子:( 提前致谢。
如果你想将 7 个 Maybe String
值的 List
转换为 ResponseLine
对象,那么类似下面的东西应该可以工作:
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"
如果 Maybe String
值列表的长度不是 7,我将由您来决定您需要什么错误处理。另请注意,我已将参数声明为 getResponseLine
作为类型 List (Maybe String)
而不是 List Maybe String
:编译器会将后者解释为 List
是一种采用两个类型变量的类型,类型变量的值为 Maybe
和 String
。
我怀疑内存不足错误是在 Elm 编译器试图找出您对 foldl
的调用中的类型时引起的。它看起来与 this Elm compiler bug.