Created
September 23, 2015 14:04
-
-
Save wfaler/de653ce89bd46fc307b6 to your computer and use it in GitHub Desktop.
Reader nested with IO and other [somemonad]T's
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.Trans.Reader | |
import Control.Monad.Trans.Maybe | |
import Control.Monad.IO.Class | |
import Control.Monad.Trans | |
--import Control.Monad.State | |
m :: IO () | |
m = do | |
runReaderT r2 5 | |
putStrLn "done" | |
r2 :: ReaderT Int IO () | |
r2 = do | |
res <- runMaybeT $ do | |
r <- respond2 | |
b <- respond3 | |
return (r ++ b) | |
list <- respond4 | |
lift $ putStrLn $ show list | |
lift $ putStrLn $ (show $ res) ++ " in r2" | |
respond2 :: MaybeT (ReaderT Int IO) String | |
respond2 = do | |
e <- lift $ (ask :: (ReaderT Int IO) Int) | |
liftIO $ putStrLn "in respond2" | |
bar <- MaybeT $ return $ Just ("foo" ++ (show e)) | |
return bar | |
respond3 :: MaybeT (ReaderT Int IO) String | |
respond3 = do | |
e <- lift $ (ask :: (ReaderT Int IO) Int) | |
liftIO $ putStrLn "in respond3" | |
bar <- MaybeT $ return $ Just ("foo" ++ (show e)) | |
return bar | |
-- how to do with other than transformers? | |
respond4 :: (ReaderT Int IO) [String] | |
respond4 = do | |
e <- (ask :: (ReaderT Int IO) Int) | |
return $ ["foo", "bar", show e] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment