Optics

To understand what problem optics solve and why they are an essential tool in the FP toolbox, read Thomas Honeyman's Practical Profunctor Lenses & Optics in PureScript.

To learn how to use lenses...

Below are other resources that are more reference material than clear explanations to beginners:

If you're looking to generate optics for your types, consider purescript-tidy-codgen-lens.

Lenses

Note: due to Thomas' above blog post, the below section will be removed in the next major release.

Since FP data types cannot be subclassed like OO data types, one will often define a shared component and then define its 'subclasses' as having that shared component:

type Shape = { fill :: Color }
type Square = { width :: Number
              , height :: Number
              , shape :: Shape
              }
type Circle = { radius :: Number
              , shape :: Shape
              }

As a result, when we wish to update the shape part of a Square or Circle, we need to deal with all that nesting:

-- for example...
user.config.personal.privacy.email { isPublic = false }

This leads to a lot of boilerplate but the concept is easily abstracted into Lenses.