Last active
July 23, 2016 19:16
-
-
Save jdoiwork/bc01edc1339f4f843d345106c3a63451 to your computer and use it in GitHub Desktop.
IO (Either e a) を ExceptT を使ってうまいことする
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
module Main where | |
import Control.Monad.Except | |
type IOE a = IO (Either String a) | |
req1 :: IOE Int | |
req1 = return . Right $ 1 | |
req2 :: Int -> IOE Int | |
req2 n = return . Right $ n + 2 | |
-- req2 n = return . Left $ "error" | |
req3 :: Int -> IOE Int | |
req3 n = return . Right $ n + 3 | |
main :: IO () | |
main = do | |
a <- runExceptT $ do | |
n1 <- ExceptT $ req1 | |
n2 <- ExceptT $ req2 n1 | |
n3 <- ExceptT $ req3 n2 | |
return n3 | |
b <- do | |
en1 <- req1 | |
en2 <- case en1 of | |
Left e -> return $ Left e | |
Right n1 -> req2 n1 | |
en3 <- case en2 of | |
Left e -> return $ Left e | |
Right n2 -> req3 n2 | |
return en3 | |
putStrLn $ show a | |
putStrLn $ show b | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment