September 2017
Beginner to intermediate
396 pages
9h 46m
English
In this recipe, we have defined a generic lens Lens s t a b which expands to the following:
fmap (setter x) $ f (getter x)
Here, x is some structure, getter x gets a field from x, and setter x is a function that takes an argument, sets the same field, and returns a modified x. In the lens, they are connected together by fmap, with f transforming the value of the field to a Functor.
Since, the lens is a rank 2 type, through the choice of f, if made wisely, we can achieve both generic get and set at the same time. Thus, when creating a generic getter view function, we use Access as a Functor. Access captures the field value, and ignores fmap mapping. On the other hand, the setter set function, uses Binder, which takes the input ...
Read now
Unlock full access