Skip to content

Instantly share code, notes, and snippets.

@phadej
Last active May 10, 2016 09:56
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 phadej/897fc3a9bee237938f134eb34db7033b to your computer and use it in GitHub Desktop.
Save phadej/897fc3a9bee237938f134eb34db7033b to your computer and use it in GitHub Desktop.
-- | 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