module Control.Concurrent.Bag.BagT
( BagT (..)
, getResult
, getAllResults )
where
import Control.Applicative
import Control.Monad.Reader
newtype BagT r m a = BagT { getBagReader :: ReaderT (m (Maybe r)) m a }
instance Monad m => Functor (BagT r m) where
fmap = liftM
instance Monad m => Applicative (BagT r m) where
pure = return
(<*>) = ap
instance Monad m => Monad (BagT r m) where
return = BagT . return
(BagT a) >>= b = BagT $ a >>= getBagReader . b
instance MonadTrans (BagT r) where
lift act = BagT $ lift act
instance MonadIO m => MonadIO (BagT r m) where
liftIO act = BagT $ liftIO act
getResult :: (MonadIO m) => BagT r m (Maybe r)
getResult = do
gr <- BagT $ ask
lift gr
getAllResults :: (MonadIO m) => BagT a m [a]
getAllResults = do
mx <- getResult
case mx of
Just x -> do
xs <- getAllResults
return $ x:xs
Nothing ->
return []