Created
October 4, 2013 13:59
-
-
Save hadronized/6826350 to your computer and use it in GitHub Desktop.
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
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