Version 5 (modified by blamario, 6 years ago)



The coroutine-iteratee package can be used as a bridge between the iteratee and monad-coroutine packages. It provides two-way conversion functions between an Iteratee and an Await-suspending coroutine, and also between an Enumerator and a Yield-suspending coroutine.

The function signatures are compatible with the coroutine-enumerator package, so the two packages can be combined into a bridge between the existing enumerator-based and iteratee-based libraries:

import Data.Monoid (Monoid)
import qualified Data.Enumerator as E
import qualified Data.Iteratee as I
import qualified Control.Monad.Coroutine.Enumerator as CE
import qualified Control.Monad.Coroutine.Iteratee as CI
import Control.Monad.Coroutine (Coroutine)
import Control.Monad.Coroutine.SuspensionFunctors (Yield)

iterateeI2E :: (Monoid s, Monad m) => I.Iteratee s m a -> E.Iteratee s m a
iterateeI2E = CE.coroutineIteratee . CI.iterateeCoroutine

iterateeE2I :: (Monoid s, Monad m) => E.Iteratee s m a -> I.Iteratee s m a
iterateeE2I = CI.coroutineIteratee . CE.iterateeCoroutine
enumeratorI2E :: (Monoid s, Monad m) => I.Enumerator s (Coroutine (Yield [s]) m) () -> E.Enumerator s m ()
enumeratorI2E = CE.coroutineEnumerator . CI.enumeratorCoroutine

enumeratorE2I :: (Monoid s, Monad m) => E.Enumerator s (Coroutine (Yield [s]) m) () -> I.Enumerator s m ()
enumeratorE2I = CI.coroutineEnumerator . CE.enumeratorCoroutine

Here's an example that uses enumInflate from the (iteratee-based) package iteratee-compress, together with the enumerator-based function iterHandle, to build an iteratee that writes data into a compressed file:

import Control.Monad ((>=>))
import System.IO (Handle, withBinaryFile, IOMode(WriteMode))
import Control.Monad.IO.Class (MonadIO(liftIO))
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)
import Data.Enumerator.IO (iterHandle)
import Data.Iteratee.ZLib (enumDeflate, Format(GZip), defaultCompressParams)

iterCompressHandle :: MonadIO m => Handle -> m (I.Iteratee ByteString m ())
iterCompressHandle = enumDeflate GZip defaultCompressParams . iterateeE2I . iterHandle

main = withBinaryFile "hello.gz" WriteMode $
       \h-> iterCompressHandle h
       >>= I.enumPure1Chunk (pack "Hello, World!\n")
       >>= foldr (>=>) return (replicate 10 $ I.enumPure1Chunk (pack "Salut, Monde!\n"))