Created
March 24, 2019 13:17
-
-
Save dmwit/c764c9b46b08c9aa88fd2d3a5f1f010a to your computer and use it in GitHub Desktop.
applicative initialization
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.Writer hiding (liftIO) | |
import Data.Functor.Compose | |
type InitIO = Compose (Writer (IO ())) IO | |
-- | Do no initialization | |
liftIO :: IO a -> InitIO a | |
liftIO = Compose . pure | |
liftInit :: IO () -> InitIO () | |
liftInit act = Compose (return () <$ tell act) | |
initializeAndRun :: InitIO a -> IO a | |
initializeAndRun act = case runWriter (getCompose act) of | |
(go, init) -> init >> go | |
open :: String -> InitIO () | |
open s | |
= liftInit (putStrLn ("initializing " ++ s)) | |
*> liftIO (putStrLn ("running " ++ s)) | |
main = initializeAndRun | |
$ open "dialog" | |
*> open "window" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment