Created
May 20, 2017 22:14
-
-
Save anonymous/089e6f9a6e1553098780ec8497ee09c7 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
import Control.Monad.Cont | |
import Control.Monad.Except | |
import Control.Concurrent | |
type Promise a = | |
ContT () (ExceptT String IO) a | |
run :: Promise a -> (a -> IO ()) -> IO () | |
run (ContT x) h = | |
either putStrLn return =<< runExceptT (x (liftIO . h)) | |
father :: String -> Promise String | |
father name = ContT (\resolve -> do | |
-- fake a request | |
liftIO (threadDelay (1000 * 1000)) | |
case name of | |
"Bob" -> resolve "Joe" | |
"Joe" -> resolve "Phil" | |
"Phil" -> resolve "Eric" | |
_ -> throwError "Not in database" | |
return ()) | |
grandFather p = do | |
f <- father p | |
g <- father f | |
return g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment