Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update an inner record in elm

Tags:

elm

I have this model

type alias Model = 
  { exampleId : Int
  , groupOfExamples : GroupExamples
  }

type alias GroupExamples = 
  { groupId : Int
  , results : List String
  }

In my update function, if I want to update the exampleId would be like this:

 { model | exampleId = updatedValue }

But what if I need to do to update, for example, just the results value inside of GroupExamples?

like image 204
Mattos Avatar asked Jan 23 '16 01:01

Mattos


1 Answers

The only way to do it in the language without anything extra is to destructure the outer record like:

let
    examples = model.groupOfExamples
    newExamples = { examples | results = [ "whatever" ] }
in
    { model | groupOfExamples = newExamples }

There is also the focus package which would allow you to:

set ( groupOfExamples => results ) [ "whatever" ] model
like image 68
robertjlooby Avatar answered Sep 27 '22 23:09

robertjlooby