An experiment with functional optics[fn:1].
A lens is a value that composes a getter and a setter function to produce a bidirectional view into a data structure. This definition is intentionally broad—lenses are a very general concept, and they can be applied to almost any kind of value that encapsulates data. – Racket ‘lens’ documentation
To construct a lens:
# A lens for the key :name
Focus.Lens.make_lens(:name)
# A lens for the key "name"
Focus.Lens.make_lens("name")
# A lens for the second item in a list/tuple:
Focus.Lens.make_lens(1)
Each lens provides both a getter and a setter for the accessor it was created for.
Lenses can be used to access and/or modify structured data:
# Extract a value from a simple map:
person = %{name: "Homer"}
nameLens = Focus.Lens.make_lens(:name)
Focus.Lens.view!(nameLens, person) == "Homer"
# true
Focus.Lens.set(nameLens, person, "Bart")
# %{name: "Bart"}
Focus.Lens.over(nameLens, person, &String.upcase/1)
# %{name: "HOMER"}
Their real utility comes in operating on nested data. Lenses can be created by composing other lenses in order to traverse a data structure:
person = %{
name: "Homer",
address: %{
locale: %{
number: 742,
street: "Evergreen Terrace",
city: "Springfield",
},
state: "???"
}
}
# To access the street, we can compose the lenses that lead there from the top level.
# Lenses can be composed with Focus.Lens.compose/2, or the infix (~>) operator.
address = Focus.Lens.make_lens(:address)
locale = Focus.Lens.make_lens(:locale)
street = Focus.Lens.make_lens(:street)
address
~> locale
~> street
|> Focus.Lens.view!(person)
# "Evergreen Terrace"
address
~> locale
~> street
|> Focus.Lens.set(person, "Fake Street")
# person = %{
# name: "Homer",
# address: %{
# locale: %{
# number: 742,
# street: "Fake Street",
# city: "Springfield",
# },
# state: "???"
# }
# }
Focus.Lens.make_lens/1
Focus.Lens.view!/2
Focus.Lens.view/2
Focus.Lens.set/3
Focus.Lens.over/3
Focus.Lens.apply_list/2
Focus.Lens.compose/2, (~>)
Focus.Lens.alongside/2
- Add
focus
to your list of dependencies inmix.exs
:def deps do [{:focus, "~> 0.1.0"}] end
- Ensure
focus
is started before your application:def application do [applications: [:focus]] end
[fn:1] This library currently combines Lenses, Prisms, and Traversals in its implementation of Lens.