lens: Lenses, Folds and Traversals
The combinators in
Control.Lens provide a highly generic toolbox for composing
families of getters, folds, isomorphisms, traversals, setters and lenses and their indexed variants.
For a longer description of why you should care about lens families, and an overview of why we use 4 parameters a, b, c, and d instead of just 2, see http://comonad.com/reader/2012/mirrored-lenses/.
Sometimes you won't need the flexibility those extra parameters afford you and you can use
type Simple f a b = f a a b b
to describe a
Note: If you merely want your library to provide lenses you may not
have to actually import any lens library at all. For, say, a
, just export a function with the signature:
Lens Bar Foo
foo :: Functor f => (Foo -> f Foo) -> Bar -> f Bar
and then you can compose it with other lenses using nothing more than
(.) from the Prelude.
You can derive lenses automatically for many data types using
makeLenses, and if a
container is fully characterized by its lenses, you can use