Last active
September 6, 2017 16:56
-
-
Save IronGremlin/c7f8dfc96d10d4c68267e9cd13133061 to your computer and use it in GitHub Desktop.
sample StateT
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 GeneralizedNewtypeDeriving #-} | |
module Example () where | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Control.Monad.State.Strict | |
type MyAppState = Int -- Your state implementation here. | |
newtype MyApp a = MyApp { runMyApp :: StateT MyAppState IO a } | |
deriving (Functor, Applicative, Monad, MonadState MyAppState, MonadIO) | |
initAppState :: MyAppState | |
initAppState = 0 -- dummy placeholder for empty state, needs implementation | |
goApp :: MyApp n -> IO n | |
goApp init' = evalStateT (runMyApp init') initAppState | |
doStateStuff :: MyApp () | |
doStateStuff = do | |
liftIO . putStrLn $ "Hit Some button to increment your state counter" | |
_ <- liftIO getChar | |
modify (+1) | |
n <- get | |
liftIO . putStrLn $ "Your State counter is: "++(show n) | |
doStateStuff | |
main :: IO () | |
main = goApp doStateStuff | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment