Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Last active August 29, 2015 14:09
Show Gist options
  • Save tokiwoousaka/ace99e3a5dcb81c6b67e to your computer and use it in GitHub Desktop.
Save tokiwoousaka/ace99e3a5dcb81c6b67e to your computer and use it in GitHub Desktop.
Stateにたいして色んな処理が書けるようになったよー https://github.com/tokiwoousaka/reasonable-lens
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Monad.State
import Control.Lens
type StateIO s a = StateT s IO a
data Hoge a = Hoge
{ _foo :: a
, _bar :: String
} deriving (Show, Read, Eq, Ord)
data Piyo a b = Piyo
{ _buz :: a
, _qux :: Hoge b
} deriving (Show, Read, Eq, Ord)
makeLenses ''Hoge
makeLenses ''Piyo
piyo :: Piyo Double [Int]
piyo = Piyo
{ _buz = 0
, _qux = Hoge
{ _foo = []
, _bar = ""
}
}
main :: IO ()
main = evalStateT prog piyo
prog :: StateIO (Piyo Double [Int]) ()
prog = do
liftIO $ putStrLn "----------------------------"
traceState
buz .= 999
traceState
qux.foo .= [1,2,3]
traceState
qux .= Hoge [999] "Hello, Lens World!"
traceState
liftIO $ putStrLn "----------------------------"
qux.foo %= (99:)
traceState
buz += 1
traceState
buz -= 900
traceState
buz *= 5
traceState
buz //= 10
traceState
liftIO $ putStrLn "----------------------------"
v_buz <- use buz
v_qux <- use qux
v_quxfoo <- use $ qux.foo
liftIO $ print v_buz
liftIO $ print v_qux
liftIO $ print v_quxfoo
traceState :: StateIO (Piyo Double [Int]) ()
traceState = do
state <- get
liftIO . print $ state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment