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

[ bsd3, control, library ] [ Propose Tags ]

Please see README.md


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0
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:16:39Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 834 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-06-17 [all 1 reports]

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:

  • runBuffer / runBufferConcurrently
  • tryRunBuffer / tryRunBufferConcurrently
  • execBuffer / execBufferConcurrently
{-# 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