Skip to content

Instantly share code, notes, and snippets.

@phadej phadej/monad-classes.hs Secret
Last active May 10, 2016

Embed
What would you like to do?
-- | Use 'MonadLogger' from 'monad-logger' or something else already there in
-- the real-world application
class Monad m => MonadLog m where
log :: Text -> m ()
class Monad m => MonadProgress m where
reportProgress
:: Int -- ^ done
-> Int -- ^ all work
-> m ()
-- Trivial instances for the classes
-- IO case could be more clever, so logging and progress reporting don't infer
instance MonadLog IO where
log = Text.hPutStrLn stderr
instance MonadProgress IO where
-- Using stuff from terminal-progress-bar
reportProgress x y =
progressBar noLabel noLabel 72 (fromIntegral x) (fromIntegral y)
-- Instances for testing
instance MonadLog Identity where
log _ = pure ()
instance MonadProgress Identity where
reportProgress _ _ = pure ()
-- The beef
data Input
data Output
work
:: (MonadLog m, MonadProgress m)
=> Input
-> m Output
-- for tests:
pureWork :: Input -> Output
pureWork = runIdentity . work
-- for cli
ioWork :: Input -> IO Output
ioWork = work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.