5 karma · joined Nov 15, 2020 (6y ago) · 2 submissions
comment 6y ago · link · parent
The author himself admits that getting a value from a nested record is easy, so we don't need lenses to do that.
Setting a value from a nested record can be easily achieved using plain functions -- no need for a complex abstraction.
updatePersonDetails : (PersonDetails -> PersonDetails) -> Model -> Model
updatePersonDetails fn model =
{ model | personDetails = fn model.personDetails }
updateAddress : (Address -> Address) -> PersonDetails -> PersonDetails
updateAddress fn details =
{ details | address = fn details.address }
updateLine1 : (String -> String) -> Address -> Address
updateLine1 fn address =
{ address | line1 = fn address.line1 }
Notice that these functions are almost identical to the ones he wrote in order to build the Lenses.This update functions can be composed easily:
updatePersonDetailsAddressLine1 : (String -> String) -> Model -> Model
updatePersonDetailsAddressLine1 fn model =
updatePersonDetails (updateAddress (updateLine1 fn)) model
or, if you are feeling fancy: updatePersonDetailsAddressLine1 =
updatePersonDetails << updateAddress << updateLine1
On the other hand, nesting records is discouraged because it brings no benefit at all and it creates the problem of having to write extra boilerplate. He wouldn't even have the problem that lenses solve if he had just stuck to the "flat" record.Btw: I think it was a great article! I didn't mean to sound too harsh criticizing; just wanted to clarify that point.