Skip to content

Instantly share code, notes, and snippets.

@hadronized
Created October 4, 2013 13:59
Show Gist options
  • Save hadronized/6826350 to your computer and use it in GitHub Desktop.
Save hadronized/6826350 to your computer and use it in GitHub Desktop.
module Test where
import Control.Monad.Trans
import Control.Monad.Trans.Reader
data Env = Env {
-- |A very important value.
important :: Int
-- |A less important value.
, notImportant :: Float
} deriving (Eq,Show)
type Application = ReaderT Env IO ()
main :: IO ()
main = do
putStrLn "running a test example with ReaderT monad transformer"
start
putStrLn "quitting"
start :: IO ()
start = do
let env = Env 1337 3.14
runReaderT mainloop env
mainloop :: Application
mainloop = do
oneThing
choice <- lift $ do
putStrLn "continue? (yes/no)"
getLine
maybeLoop choice
where maybeLoop c
| c == "yes" = mainloop
| otherwise = return ()
oneThing :: Application
oneThing = do
i <- asks important
ni <- asks notImportant
lift $ do
putStrLn $ "Important value: " ++ show i ++ "\n"
++ "Less important value: " ++ show ni
-- run another thing with an altered context
local newEnv anotherThing
where newEnv e = e { important = 9000 }
anotherThing :: Application
anotherThing = do
i <- asks important
lift $ putStrLn $ "Woah, now important=" ++ show i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment