Skip to content

Instantly share code, notes, and snippets.

@ultrox
Forked from s-m-i-t-a/Main.elm
Created July 29, 2023 09:11
Show Gist options
  • Save ultrox/cb6fa3501cb37ae2a45f2bef4c808f86 to your computer and use it in GitHub Desktop.
Save ultrox/cb6fa3501cb37ae2a45f2bef4c808f86 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