Skip to content

Instantly share code, notes, and snippets.

@s-m-i-t-a
Created July 14, 2018 05:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save s-m-i-t-a/2a83c0bc5b7d7081b019d18520ebc62c to your computer and use it in GitHub Desktop.
Save s-m-i-t-a/2a83c0bc5b7d7081b019d18520ebc62c to your computer and use it in GitHub Desktop.
How to update nested fields in record
module Main exposing (..)
type alias Bar =
{ baz : String }
type alias Foo =
{ bar : Bar }
type alias Model =
{ foo : Foo }
model : Model
model =
{ foo = { bar = { baz = "hello world" } } }
setFoo : (Foo -> Foo) -> Model -> Model
setFoo fn model =
{ model | foo = fn model.foo }
setBar : (Bar -> Bar) -> Foo -> Foo
setBar fn foo =
{ foo | bar = fn foo.bar }
setBaz : String -> Bar -> Bar
setBaz str bar =
{ bar | baz = str }
updateBaz : String -> Model -> Model
updateBaz str =
(setFoo <| setBar <| setBaz str)
update : Model
update =
model |> updateBaz "foo bar baz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment