Skip to content

Instantly share code, notes, and snippets.

@IronGremlin
Last active September 6, 2017 16:56
Show Gist options
  • Save IronGremlin/c7f8dfc96d10d4c68267e9cd13133061 to your computer and use it in GitHub Desktop.
Save IronGremlin/c7f8dfc96d10d4c68267e9cd13133061 to your computer and use it in GitHub Desktop.
sample StateT
{-# 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