https://github.com/dcastro/safe-buffer-monad
https://github.com/dcastro/safe-buffer-monad
haskell haskell-library monad monad-transformers
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dcastro/safe-buffer-monad
- Owner: dcastro
- License: bsd-3-clause
- Created: 2018-05-25T16:30:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-19T19:57:33.000Z (about 7 years ago)
- Last Synced: 2025-02-08T03:51:15.260Z (5 months ago)
- Topics: haskell, haskell-library, monad, monad-transformers
- Language: Haskell
- Homepage: http://hackage.haskell.org/package/safe-buffer-monad
- Size: 22.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.```haskell
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````haskell
{-# 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
``````plain
λ> main
brace for impact!
*** Exception: user error (boom)λ> :! tail log.txt
line 1
line 2
```