Template Haskell generation of profunctor optics from record types.
Automatically derive lenses, prisms, traversals, and other optics from data type declarations using Template Haskell.
A lens focuses on exactly one field of a product type. For a record with fields, each field naturally gives rise to a lens. Template Haskell automates the boilerplate.
{-# LANGUAGE TemplateHaskell #-}
import Data.Profunctor.Optic.TH
data Person = Person
{ _personName :: String
, _personAge :: Int
}
makeFieldOptics defaultFieldRules ''Person
-- Generates:
-- personName :: Lens' Person String
-- personAge :: Lens' Person Int
>>> view personName (Person "Alice" 30)
"Alice"
>>> set personAge 31 (Person "Alice" 30)
Person "Alice" 31
{-# LANGUAGE TemplateHaskell #-}
import Data.Profunctor.Optic.TH
data Shape
= Circle Double
| Rectangle Double Double
| Triangle Double Double Double
makeFieldOptics defaultFieldRules ''Shape
-- Generates prisms for each constructor
| Module | Contents |
|---|---|
Data.Profunctor.Optic.TH |
makeFieldOptics, defaultFieldRules, makePrismOptics |
base, profunctor-optics, template-haskell, th-abstraction