O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required