Created
December 5, 2021 02:34
-
-
Save ahaxu/e88f821c19b0c6d6e51ee9ab48e8da84 to your computer and use it in GitHub Desktop.
Combination of readerT and maybeT
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 CombineMonadT where | |
import Control.Monad.Trans | |
import Control.Monad.Trans.Maybe | |
import Control.Monad.Trans.Reader | |
getName :: MaybeT (ReaderT String IO) String | |
getName = MaybeT $ do | |
input <- ask | |
lift $ putStrLn input | |
name <- lift $ getLine | |
if name == "" | |
then return Nothing | |
else return $ Just name | |
getName' :: ReaderT String (MaybeT IO) String | |
getName' = ReaderT $ | |
\r -> MaybeT $ do | |
putStrLn r | |
name <- getLine | |
if name == "" | |
then return Nothing | |
else return $ Just name | |
-- https://stackoverflow.com/questions/43840588/use-readert-maybe-or-maybet-reader | |
main :: IO () | |
main = do | |
mName <- runReaderT (runMaybeT getName) "type your name" | |
case mName of | |
Just name -> putStrLn $ "Hello " ++ name | |
Nothing -> putStrLn "oh noooo!" | |
nName' <- runMaybeT $ (runReaderT getName') "your name" | |
case nName' of | |
Just n -> putStrLn $ "Hello " ++ n | |
Nothing -> putStrLn "oh noooo!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment