safe-buffer-monad: A monadic buffer resilient to exceptions

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Please see README.md


[Skip to Readme]

Properties

Versions 0.1.0, 0.1.0
Change log None available
Dependencies base (>=4.7 && <5), mtl (>=2.0 && <3.0), safe-exceptions (>=0.1 && <1.0), stm (>=2.0 && <3.0) [details]
License BSD-3-Clause
Copyright 2017 Diogo Castro
Author Diogo Castro
Maintainer dc@diogocastro.com
Category Control
Home page https://github.com/dcastro/safe-buffer-monad#readme
Bug tracker https://github.com/dcastro/safe-buffer-monad/issues
Source repo head: git clone https://github.com/dcastro/safe-buffer-monad
Uploaded by dcastro at 2018-06-17T19:13:42Z

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for safe-buffer-monad-0.1.0

[back to package description]

safe-buffer-monad

A monadic buffer resilient to exceptions.

The SafeBufferMonad typeclass models a buffer that you can write things to. If an exception is thrown, you'll still be able to proccess the contents of the buffer up to the point where the computation was interrupted.

class Monad m => SafeBufferMonad s m | m -> s where
  readBuffer   :: m s
  writeBuffer  :: s -> m ()
  clearBuffer  :: m s
  modifyBuffer :: (s -> s) -> m ()

The buffer can be run using one of these 6 functions:

{-# LANGUAGE FlexibleContexts #-}

import SafeBuffer
import Data.List (intercalate)

go :: (SafeBufferMonad [String] m, MonadIO m) => m String
go = do
  writeBuffer ["line 1"]
  writeBuffer ["line 2"]
  liftIO $ putStrLn "brace for impact!"
  liftIO $ throwIO $ userError "boom"
  writeBuffer ["line 3"]
  pure "done!"

main = runBuffer (appendFile "log.txt" . intercalate "\n") go
λ> main
brace for impact!
*** Exception: user error (boom)

λ> :! tail log.txt
line 1
line 2