<commit_before>module Reactive.Bacon.Monadic where

import Data.IORef
import Reactive.Bacon
import Control.Concurrent.STM
import Control.Monad

instance Monad Observable where
  return x = getObservable [x]
  (>>=) = selectManyE

selectManyE :: Source s => s a -> (a -> Observable b) -> Observable b
selectManyE xs binder = Observable $ \(Observer sink) -> do
    state <- newTVarIO $ State sink Nothing 1 [] False
    dispose <- subscribe (obs xs) $ Observer $ mainEventSink state
    atomically $ modifyTVar state $ \state -> state { dispose = Just dispose }
    return dispose
  where mainEventSink state eventA = do
            case eventA of
              End    -> do
                (end, sink) <- withState state $ \s -> do
                    modifyTVar state $ \s -> s { mainEnded = True }
                    return (null (children s), currentSink s)
                when end $ void $ sink End
                return NoMore<commit_msg>corrected dispose of selectManyE<commit_after>module Reactive.Bacon.Monadic where

import Data.IORef
import Reactive.Bacon
import Control.Concurrent.STM
import Control.Monad

instance Monad Observable where
  return x = getObservable [x]
  (>>=) = selectManyE

selectManyE :: Source s => s a -> (a -> Observable b) -> Observable b
selectManyE xs binder = Observable $ \(Observer sink) -> do
    state <- newTVarIO $ State sink Nothing 1 [] False
    dispose <- subscribe (obs xs) $ Observer $ mainEventSink state
    atomically $ modifyTVar state $ \state -> state { dispose = Just dispose }
    return $ disposeAll state
  where mainEventSink state eventA = do
            case eventA of
              End    -> do
                (end, sink) <- withState state $ \s -> do
                    modifyTVar state $ \s -> s { mainEnded = True }
                    return (null (children s), currentSink s)
                when end $ void $ sink End
                return NoMore
{'fraction_non_alphanumeric': 0.08688865764828303, 'fraction_numerical': 0.001040582726326743, 'mean_word_length': 2.2373737373737375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '900555', 'n_tokens_mistral': 554, 'n_tokens_neox': 520, 'n_words': 195}