Skip to content

Instantly share code, notes, and snippets.

@thumphries
Created October 4, 2017 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thumphries/6fe8342d23b3182baa682e284edeb87c to your computer and use it in GitHub Desktop.
Save thumphries/6fe8342d23b3182baa682e284edeb87c to your computer and use it in GitHub Desktop.
Simulating first-class patterns by combining prisms into `Getting First`
{-# 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