Elm 简化记录的递归更新

Elm 简化记录的递归更新,elm,Elm,这个代码可以简化吗 update : Action -> Model -> Model update action model = let formValue = model.formValue in case action of UpdateWhat what -> let newValue = { formValue | what <- what } in {

这个代码可以简化吗

update : Action -> Model -> Model
update action model =
  let
    formValue = model.formValue
  in
    case action of
      UpdateWhat what ->
        let
          newValue = { formValue | what <- what }
        in
          { model | formValue <- newValue }
      UpdateTrigger trigger ->
        let
          newValue = { formValue | trigger <- trigger }
        in
          { model | formValue <- newValue }
更新:操作->模型->模型
更新动作模型=
让
formValue=model.formValue
在里面
案例诉讼
更新什么->
让

newValue={formValue | what我想你要找的是:

集中 焦点是处理大数据块的特定部分的一种方法。在最基本的层面上,它允许您以简单且可组合的方式获取和设置记录的字段。这意味着您可以避免编写特殊的记录更新语法,并使用更优雅的组合

它使您能够在以下代码段中编写冻结之类的内容:

mario =
    { super = False
    , fire  = False
    , physics = { position = { x=3, y=4 }
                , velocity = { x=1, y=1 }
                }
    }

freeze object =
    set (physics => velocity) { x=0, y=0 } object
在代码示例中,物理和速度是焦点。您可以使用以下代码创建焦点,以使用您的示例:

formValue = Focus.create .formValue (\f r -> { r | formValue <- f r.formValue })
what      = Focus.create .what      (\f r -> { r | what      <- f r.what })
trigger   = Focus.create .trigger   (\f r -> { r | trigger   <- f r.trigger })

update : Action -> Model -> Model
update action model =
  case action of
    UpdateWhat w -> Focus.set (formValue => what) w model
    UpdateTrigger t -> Focus.set (formValue => trigger) t model
formValue=Focus.create.formValue(\f r->{r | formValue{r | what{r | trigger Model->Model
更新动作模型=
案例诉讼
UpdateWhat w->Focus.set(formValue=>what)w模型
UpdateTrigger t->Focus.set(formValue=>trigger)t模型
谢谢,这很有效: