Created
June 12, 2019 14:31
-
-
Save pradyuman/6ee9e0573e6a0890bdae18203f6294ac to your computer and use it in GitHub Desktop.
Nested Maybe Accessor (Haskell)
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 #-} | |
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