Working with Prism
Lens gives you the ability to focus on a particular field in a data type. Traversal will do the same thing for a traversable (something that you can traverse and collect). But these data types were product types.
In this recipe, we will work with Prism, where we will work with sum type data.
How to do it...
- Create a new project
working-with-prism
with asimple
stack template:
stack new working-with-prism simple
- Add a dependency on the
lens
library in thebuild-depends
sub-section of theexecutable
section:
executable working-with-prism hs-source-dirs: src main-is: Main.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , lens
- Open
src/Main.hs
. We will be adding our source here. EnableTemplateHaskell
, as we will produce Prism using the template Haskell. Add theMain
module. Import the template haskell module for lens, alongwith other imports:
{-# LANGUAGE...