Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created March 24, 2019 13:17
Show Gist options
  • Save dmwit/c764c9b46b08c9aa88fd2d3a5f1f010a to your computer and use it in GitHub Desktop.
Save dmwit/c764c9b46b08c9aa88fd2d3a5f1f010a to your computer and use it in GitHub Desktop.
applicative initialization
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