Elm:从字符串列表中提取记录值

时间:2016-06-21 14:29:31

标签: functional-programming elm

我尝试创建一个记录和属性名列表的函数,并将值作为字符串列表返回:

Model -> (List String) -> (List String)

目标是稍后能够将记录动态呈现到HTML表中:

Model -> (List String) -> (Html Cmd)

我成功实现了这个:

import Html exposing (text)

type Field
  = FieldInt Int
  | FieldString String

type alias Model = 
  { name: Field
  , age: Field
  }

user1 = Model (FieldString "foo") (FieldInt 3)

field2text: Field -> String
field2text field =
  case field of
    FieldInt value ->
      toString value
    FieldString value ->
      value

updateModel: (List (a -> Field)) -> (List String)
model2values fns model =
  List.map (\fn -> field2text <| fn model) fns

main =
  text <| toString (model2values [.name, .age] user1)

如何更新model2values函数以便能够将最后一行更改为:

main =
  text <| toString (model2values ["name", "age"] user1)

1 个答案:

答案 0 :(得分:3)

没有好办法,所以你只剩下:

NEWOLD