Often, I find myself writing code like this:
InputAdded path newInput ->
{ model
| projects =
model.projects
|> ZipList.updateSelected
(\project ->
{ project
| calls =
project.calls
|> List.map
(Tree.updateAt path
(Tree.prependChild newInput)
)
}
)
}which is ... suboptimal.
Using lenses:
import Field
import Accessors.Library as Accessors
InputAdded path newInput ->
model
|> over
((Field.projects << ZipList.selected)
<< Field.calls
<< Accessors.onEach
)
(Tree.updateAt path
(Tree.prependChild newInput)
)the code becomes more readable.
The biggest pain-point with these lenses is the need to manually create one for every used field.
→ This elm-review rule generates all those record field lenses that don't exist yet.
In this example: Field.projects and Field.calls will automatically be generated in Field.elm.
Note: It's also possible to generate custom lenses or to customize the generation of existing ones.
module ReviewConfig exposing (config)
import NoMissingRecordFieldLens
import Review.Rule exposing (Rule)
config : List Rule
config =
[ NoMissingRecordFieldLens.rule
{ generate = NoMissingRecordFieldLens.accessors
, generateIn =
( "Accessors", [ "Library", "RecordFields" ] )
}
]See Config
→ contributing.