Created
February 20, 2012 08:51
-
-
Save nurpax/1868472 to your computer and use it in GitHub Desktop.
Reader + IO monad transformer
This file contains hidden or 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
import Control.Monad.Reader | |
data Db = Db { conn :: String } | |
type DbIO = ReaderT Db IO | |
insert :: String -> DbIO () | |
insert s = do | |
conn <- asks conn | |
liftIO $ putStrLn ("Using connection " ++ conn ++ " inserting " ++ s) | |
run :: DbIO () | |
run = do | |
conn <- asks conn | |
liftIO $ putStrLn ("Using connection " ++ conn) | |
insert "foo" | |
return () | |
dbContext = | |
Db "this is my cnxn" | |
main :: IO () | |
main = do | |
(runReaderT run dbContext) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment