module Data.Enumerator.Trans
(
runIdentityI
, runMaybeI
, runErrorI
, runReaderI
, runStateI
, evalStateI
, runStateI'
, evalStateI'
, runWriterI
, execWriterI
, runWriterI'
, execWriterI'
, runRWSI
, evalRWSI
, execRWSI
, runRWSI'
, evalRWSI'
, execRWSI'
) where
import Data.Monoid (Monoid(..))
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Error
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.State.Lazy as L
import qualified Control.Monad.Trans.State.Strict as S
import qualified Control.Monad.Trans.Writer.Lazy as L
import qualified Control.Monad.Trans.Writer.Strict as S
import qualified Control.Monad.Trans.RWS.Lazy as L
import qualified Control.Monad.Trans.RWS.Strict as S
import Data.Enumerator (Stream(..), Step(..), Iteratee(..))
runIdentityI :: Monad m => Iteratee a (IdentityT m) b -> Iteratee a m b
runIdentityI it = Iteratee $ do
step <- runIdentityT $ runIteratee it
return $ case step of
Continue k -> Continue $ runIdentityI . k
Yield x cs -> Yield x cs
Error e -> Error e
runMaybeI :: Monad m => Iteratee a (MaybeT m) b -> Iteratee a m (Maybe b)
runMaybeI it = Iteratee $ do
mStep <- runMaybeT $ runIteratee it
return $ case mStep of
Nothing -> Yield Nothing $ Chunks []
Just step -> case step of
Continue k -> Continue $ runMaybeI . k
Yield x cs -> Yield (Just x) cs
Error e -> Error e
runErrorI :: (Error e, Monad m)
=> Iteratee a (ErrorT e m) b -> Iteratee a m (Either e b)
runErrorI it = Iteratee $ do
mStep <- runErrorT $ runIteratee it
return $ case mStep of
Left e -> Yield (Left e) $ Chunks []
Right step -> case step of
Continue k -> Continue $ runErrorI . k
Yield x cs -> Yield (Right x) cs
Error e -> Error e
runReaderI :: Monad m => r -> Iteratee a (ReaderT r m) b -> Iteratee a m b
runReaderI r it = Iteratee $ do
step <- runReaderT (runIteratee it) r
return $ case step of
Continue k -> Continue $ runReaderI r . k
Yield x cs -> Yield x cs
Error e -> Error e
runStateI :: Monad m => s -> Iteratee a (L.StateT s m) b -> Iteratee a m (b, s)
runStateI s it = Iteratee $ do
~(step, s') <- L.runStateT (runIteratee it) s
return $ case step of
Continue k -> Continue $ runStateI s' . k
Yield x cs -> Yield (x, s') cs
Error e -> Error e
evalStateI :: Monad m => s -> Iteratee a (L.StateT s m) b -> Iteratee a m b
evalStateI s = fmap fst . runStateI s
runStateI' :: Monad m => s -> Iteratee a (S.StateT s m) b -> Iteratee a m (b, s)
runStateI' s it = Iteratee $ do
(step, s') <- S.runStateT (runIteratee it) s
return $ case step of
Continue k -> Continue $ runStateI' s' . k
Yield x cs -> Yield (x, s') cs
Error e -> Error e
evalStateI' :: Monad m => s -> Iteratee a (S.StateT s m) b -> Iteratee a m b
evalStateI' s = fmap fst . runStateI' s
runWriterI :: (Monoid w, Monad m)
=> Iteratee a (L.WriterT w m) b -> Iteratee a m (b, w)
runWriterI it0 = go mempty it0 where
go w it = Iteratee $ do
~(step, w') <- L.runWriterT $ runIteratee it
return $ case step of
Continue k -> Continue $ go (w `mappend` w') . k
Yield x cs -> Yield (x, w `mappend` w') cs
Error e -> Error e
execWriterI :: (Monoid w, Monad m)
=> Iteratee a (L.WriterT w m) b -> Iteratee a m w
execWriterI = fmap snd . runWriterI
runWriterI' :: (Monoid w, Monad m)
=> Iteratee a (S.WriterT w m) b -> Iteratee a m (b, w)
runWriterI' it0 = go mempty it0 where
go w it = Iteratee $ do
(step, w') <- S.runWriterT $ runIteratee it
return $ case step of
Continue k -> Continue $ go (w `mappend` w') . k
Yield x cs -> Yield (x, w `mappend` w') cs
Error e -> Error e
execWriterI' :: (Monoid w, Monad m)
=> Iteratee a (S.WriterT w m) b -> Iteratee a m w
execWriterI' = fmap snd . runWriterI'
runRWSI :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (L.RWST r w s m) b -> Iteratee a m (b, s, w)
runRWSI r s0 it0 = go s0 mempty it0 where
go s w it = Iteratee $ do
~(step, s', w') <- L.runRWST (runIteratee it) r s
return $ case step of
Continue k -> Continue $ go s' (w `mappend` w') . k
Yield x cs -> Yield (x, s', w `mappend` w') cs
Error e -> Error e
evalRWSI :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (L.RWST r w s m) b -> Iteratee a m (b, w)
evalRWSI r s = fmap (\(x, _, w) -> (x, w)) . runRWSI r s
execRWSI :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (L.RWST r w s m) b -> Iteratee a m (s, w)
execRWSI r s = fmap (\(_, s', w) -> (s', w)) . runRWSI r s
runRWSI' :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (S.RWST r w s m) b -> Iteratee a m (b, s, w)
runRWSI' r s0 it0 = go s0 mempty it0 where
go s w it = Iteratee $ do
(step, s', w') <- S.runRWST (runIteratee it) r s
return $ case step of
Continue k -> Continue $ go s' (w `mappend` w') . k
Yield x cs -> Yield (x, s', w `mappend` w') cs
Error e -> Error e
evalRWSI' :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (S.RWST r w s m) b -> Iteratee a m (b, w)
evalRWSI' r s = fmap (\(x, _, w) -> (x, w)) . runRWSI' r s
execRWSI' :: (Monoid w, Monad m)
=> r -> s -> Iteratee a (S.RWST r w s m) b -> Iteratee a m (s, w)
execRWSI' r s = fmap (\(_, s', w) -> (s', w)) . runRWSI' r s