Nested data structures with functional lenses
The concept I’m going to describe here is far from new, however I found that there wasn’t a good Elixir library that implemented it so I wrote one (https://hex.pm/packages/lens). If you’re familiar with lenses, then you’ll probably not find anything new in the post, just go and use the library.
Still here? Great. So what does lens
do? You can think about this like Elixir’s get_in/update_in
on steroids. Where in the case of get_in/update_in
you can describe a path to a single value (with the narrow exception of Access.all
), lens
allows you to describe a path to many values and get or update all of them with one function call. In fact lenses in lens
are compatible with get_in/update_in
. Also note that when I say “update” I mean that in the functional sense - create a new datastructure that’s mostly the same except for the parts we’re “updating”.
For example you can use Lens.filter
like A
...