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 our last recipe, we manufactured our own lenses. A lens is defined as follows:

    type Lens s t a b = forall f . Functor f => (a -> f b) -> s -> f t

The creation of the lens is achieved via the creation of getter and setter for a particular field in a data type.

    lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
    lens getter setter f x = fmap (setter x) $ f (getter x)

We have to write getter and setter manually for each field to supply lenses. Using TemplateHaskell we can create these lenses. Also, note the use of "_" while defining the data type.

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