Skip to content

Instantly share code, notes, and snippets.

Created May 20, 2017 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/089e6f9a6e1553098780ec8497ee09c7 to your computer and use it in GitHub Desktop.
Save anonymous/089e6f9a6e1553098780ec8497ee09c7 to your computer and use it in GitHub Desktop.
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