Created
October 4, 2017 12:38
-
-
Save thumphries/6fe8342d23b3182baa682e284edeb87c to your computer and use it in GitHub Desktop.
Simulating first-class patterns by combining prisms into `Getting First`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Lens where | |
import Control.Lens | |
import Data.Monoid (First, (<>)) | |
data FooBar = | |
Foo (Either Int Bool) | |
| Bar (Maybe Bool) | |
makePrisms ''FooBar | |
right :: Prism' FooBar Bool | |
right = | |
_Foo . _Right | |
just :: Prism' FooBar Bool | |
just = | |
_Bar . _Just | |
pats :: Getting (First Bool) FooBar Bool | |
pats = | |
right <> just | |
bool :: FooBar -> Maybe Bool | |
bool = | |
preview pats | |
{-- | |
[1 of 1] Compiling Lens ( /tmp/Lens.hs, interpreted ) | |
Ok, modules loaded: Lens. | |
λ> bool (Foo (Right True)) | |
Just True | |
λ> bool (Bar (Just True)) | |
Just True | |
--} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment