Last active
August 29, 2015 14:08
-
-
Save tokiwoousaka/d76abdfc1f98d6a8eba8 to your computer and use it in GitHub Desktop.
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 #-} | |
| module Main where | |
| import Control.Lens | |
| import Control.Monad.State | |
| data Hoge = Hoge | |
| { _Foo :: Int | |
| , _Bar :: String | |
| } deriving (Show, Read, Eq, Ord) | |
| data Piyo = Piyo | |
| { _baz :: Hoge | |
| , _qux :: Hoge | |
| , foobar :: String | |
| } deriving (Show, Read, Eq, Ord) | |
| data Tuple a b = Tuple { _first :: a, _second :: b } deriving (Show, Read, Eq, Ord) | |
| data Fuga a b = Fuga | |
| { _tuple1 :: Tuple a Int | |
| , _tuple2 :: Tuple b Int | |
| , _quux :: String | |
| } deriving (Show, Read, Eq, Ord) | |
| hoge = Hoge | |
| { _Foo = 100 | |
| , _Bar = "HogeHoge" | |
| } | |
| piyo = Piyo | |
| { _baz = hoge | |
| , _qux = hoge | |
| , foobar = "PiyoPiyo" | |
| } | |
| fuga :: Fuga String Bool | |
| fuga = Fuga | |
| { _tuple1 = Tuple "Hoge" 10 | |
| , _tuple2 = Tuple True 20 | |
| , _quux = "Sushi" | |
| } | |
| makeLenses ''Hoge | |
| makeLenses ''Piyo | |
| makeLenses ''Tuple | |
| makeLenses ''Fuga | |
| main :: IO () | |
| main = do | |
| print piyo | |
| print $ piyo^.baz | |
| print $ piyo^.baz.bar | |
| putStrLn "-----" | |
| let piyo2 = piyo&qux.foo.~999 | |
| print piyo2 | |
| print $ piyo2^.qux | |
| putStrLn "-----" | |
| print fuga | |
| print $ fuga^.tuple1 | |
| print $ fuga^.tuple2.first | |
| putStrLn "-----" | |
| let fuga2 = fuga&tuple1.first.~[1,2,3] | |
| let fuga3 = fuga&tuple2.second.~999 | |
| let fuga4 = fuga&tuple1.~Tuple ["a", "b", "c"] 777 | |
| print fuga2 | |
| print fuga3 | |
| print fuga4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment