Lenses and their composition
Lenses are convenient abstractions for data access that are composed nicely. This way, they facilitate deep data access.
Basic lenses
In essence, we can think of a lens as a pair of two functions – one for retrieving a component of a larger data type and another for replacing that component with a new value:
data Lens' s v = MkLens' { view :: s -> v , set :: v -> s -> s }
The larger data type is sometimes called source
. Here, this is indicated with the s
type variable. The component that the lens focuses on is then called view
, which is indicated by the v
type variable here.
When using lenses, the convention is to prefix the regular field names with an underscore, and to the regular names themselves for the lenses. For example, instead of the previous definition...