-
-
Save ultrox/cb6fa3501cb37ae2a45f2bef4c808f86 to your computer and use it in GitHub Desktop.
How to update nested fields in record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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