<commit_before>{-# LANGUAGE DeriveDataTypeable, FlexibleContexts, KindSignatures, RankNTypes #-}

module Bark.Buffer (
    -- * Exported Types
      Buffer()

    -- * Conduits
    , sourceBuffer
    , sinkBuffer

    -- * Buffer Operations
    , newBuffer
    , revertBuffer

    -- * STM
    , liftSTM
    , atomically
    ) where

import Control.Monad.IO.Class         (MonadIO, liftIO)
import Control.Monad.STM              (STM, atomically)
import Control.Concurrent.STM.TBMChan
import Data.Conduit
import Data.Typeable                  (Typeable)

data Buffer a = Buffer (TBMChan a) deriving (Typeable)

sourceBuffer :: MonadIO m => Buffer a -> Source m a
sourceBuffer buf = source
    where
        source = PipeM pull close
        pull   = do
             a <- liftSTM $ readBuffer buf
             case a of
                 Nothing -> return $ Done Nothing ()
                 Just x  -> return $ HaveOutput source close x
        close  = liftSTM $ closeBuffer buf

sinkBuffer :: MonadIO m => Buffer a -> Sink a m ()
sinkBuffer buf = sink
    where
      sink       = NeedInput push close
      push input = PipeM (liftSTM $ writeBuffer buf input >> return sink) (liftSTM $ closeBuffer buf)


      close      = liftSTM $ closeBuffer buf

newBuffer :: Int -> STM (Buffer a)
newBuffer n = newTBMChan n >>= return . Buffer

revertBuffer :: Buffer a -> a -> STM ()
revertBuffer (Buffer chan) val = do
    closed <- isClosedTBMChan chan<commit_msg>Formatting<commit_after>{-# LANGUAGE DeriveDataTypeable, FlexibleContexts, KindSignatures,
    RankNTypes #-}

module Bark.Buffer (
    -- * Exported Types
      Buffer()

    -- * Conduits
    , sourceBuffer
    , sinkBuffer

    -- * Buffer Operations
    , newBuffer
    , revertBuffer

    -- * STM
    , liftSTM
    , atomically
    ) where

import Control.Monad.IO.Class         (MonadIO, liftIO)
import Control.Monad.STM              (STM, atomically)
import Control.Concurrent.STM.TBMChan
import Data.Conduit
import Data.Typeable                  (Typeable)

data Buffer a = Buffer (TBMChan a) deriving (Typeable)

sourceBuffer :: MonadIO m => Buffer a -> Source m a
sourceBuffer buf = source
    where
        source = PipeM pull close
        pull   = do
             a <- liftSTM $ readBuffer buf
             case a of
                 Nothing -> return $ Done Nothing ()
                 Just x  -> return $ HaveOutput source close x
        close  = liftSTM $ closeBuffer buf

sinkBuffer :: MonadIO m => Buffer a -> Sink a m ()
sinkBuffer buf = sink
    where
      sink       = NeedInput push (liftSTM $ closeBuffer buf)
      push input = PipeM (liftSTM $ writeBuffer buf input >> return sink)
                         (liftSTM $ closeBuffer buf)

newBuffer :: Int -> STM (Buffer a)
newBuffer n = newTBMChan n >>= return . Buffer

revertBuffer :: Buffer a -> a -> STM ()
revertBuffer (Buffer chan) val = do
    closed <- isClosedTBMChan chan
{'fraction_non_alphanumeric': 0.08616546515619636, 'fraction_numerical': 0.0, 'mean_word_length': 2.146868250539957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4856491', 'n_tokens_mistral': 878, 'n_tokens_neox': 833, 'n_words': 279}