Your last example with let / in syntax is as brief as possible in Elm 0.18 without resorting to additional packages.
As they say, in functional languages โโyou often find the concept of lenses useful for updating nested records. In arturopala / elm-monocle there is an Elm package that provides the ability to create and execute lenses to more accurately obtain and set the values โโof nested records.
Using this package, you can create lenses that allow you to do such things:
personWithUpdatedCity = personCity.set "Madrid" person getCityOfPerson = personCity.get person
The disadvantage is that you must write all the lens wiring code yourself. In Haskell, this wiring can be done by the compiler. At Elm, we donโt have that luxury.
The Elm code required for the above lenses will be as follows:
addressCityLens : Lens Address String addressCityLens = Lens .city (\cn a -> { a | city = cn }) personAddressLens : Lens Person Address personAddressLens = Lens .address (\ap -> { p | address = a }) personCity : Lens Person String personCity = compose personAddressLens addressCityLens
As you can see, this is tedious and a lot more code than you can expect to set a nested value. Because of this boredom, you can stick with the let / in example, unless your code uses nested sets everywhere.
There is an older discussion on the topic , which makes it easier to set the value in Elm, but it has been inactive for some time.
Chad gilbert
source share