Created
October 26, 2013 08:40
-
-
Save danidiaz/7166886 to your computer and use it in GitHub Desktop.
choosing from Control.Lens.Lens
This file contains hidden or 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 | |
| import Data.Tree | |
| ps :: Show a => a -> IO () | |
| ps = putStrLn . show | |
| data Person = Person { | |
| _name :: String, | |
| _years :: Int | |
| } | |
| $(makeLenses ''Person) | |
| data Dog = Dog { | |
| _dogName :: String, | |
| _dogYears :: Int | |
| } | |
| $(makeLenses ''Dog) | |
| main = do | |
| let myfold = choosing folded folded | |
| ps $ Left [1,2,3,4]^..myfold | |
| ps $ Right (Node 3 [])^..myfold | |
| let mylens = choosing years dogYears | |
| ps $ Left (Person "Wanda" 34)^.mylens | |
| ps $ Right (Dog "Spotty" 7)^.mylens | |
| let mylens' = choosing name dogName | |
| traverseOf_ mylens' putStrLn $ Left (Person "Wanda" 34) | |
| traverseOf_ mylens' putStrLn $ Right (Dog "Spotty" 7) | |
| return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment