If you have worked with object-oriented programming, then you must be aware of the properties (such as in C# or Python, or even in managed C++). Usually, we can access the properties inside an object and also set the property to some value:
Point point = Point(1.0, 2.0); double x = point.x; // Should be 1.0 point.x = 3.0; // Now point x is changed to 3.0
Though the preceding code mutates the data, it is very convenient to get and set a property. Imagine doing the same with Haskell:
data Point = Point Double Double x :: Point -> Double x (Point xv _) = xv setx :: Point -> Double -> Point setx (Point _ y) x = Point x y
We need to de-construct a type, and reconstruct it again. If we had some generic way of accessing a field inside the data, and then accessing it back, then we will get the lost convenience of getting and setting a property back.
It is said that Lens and Prism are some of the most complex pieces of code written in Haskell, thanks to the use of existential quantification, rank 2 types and many operators that can work with each other. Thankfully, working with Lens and Prism is not that hard, and definitely very productive due to their usefulness.
We will be using Edward Kmett's original lens library for this chapter. We will start by manufacturing our own lenses in the first recipe. We will quickly move on to the lens library, working with Lens, Traversal, Iso, and Prism.