Skip to content

Instantly share code, notes, and snippets.

@pradyuman
Created June 12, 2019 14:31
Show Gist options
  • Save pradyuman/6ee9e0573e6a0890bdae18203f6294ac to your computer and use it in GitHub Desktop.
Save pradyuman/6ee9e0573e6a0890bdae18203f6294ac to your computer and use it in GitHub Desktop.
Nested Maybe Accessor (Haskell)
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Parent = Parent {
_child :: Maybe Child
} deriving (Show)
data Child = Child {
_property :: Maybe String
} deriving (Show)
p :: Parent
p = Parent {
_child = Just Child {
_property = Just "hello!"
}
}
makeLenses ''Parent
makeLenses ''Child
m = _Just
(.?) a b = a . _Just . b
main :: IO ()
main = do
print p
print $ p^.child.m.property
print $ p^.child.?property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment