| 3 | | The coroutine-iteratee package can be used as a bridge between the [http://hackage.haskell.org/package/iteratee iteratee] and [wiki:monad-coroutine] packages. It provides two-way conversion functions between an Iteratee and an Await-suspending coroutine, and also |
| 4 | | between an Enumerator and a Yield-suspending coroutine. |
| | 3 | The coroutine-iteratee package can be used as a bridge between the [http://hackage.haskell.org/package/iteratee iteratee] and [wiki: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. |
| | 4 | |
| | 5 | The function signatures are compatible with the [wiki:coroutine-enumerator] package, so the two packages can be |
| | 6 | combined into a bridge between the existing enumerator-based and iteratee-based libraries: |
| | 7 | |
| | 8 | {{{ |
| | 9 | import Data.Monoid (Monoid) |
| | 10 | import qualified Data.Enumerator as E |
| | 11 | import qualified Data.Iteratee as I |
| | 12 | import qualified Control.Monad.Coroutine.Enumerator as CE |
| | 13 | import qualified Control.Monad.Coroutine.Iteratee as CI |
| | 14 | import Control.Monad.Coroutine (Coroutine) |
| | 15 | import Control.Monad.Coroutine.SuspensionFunctors (Yield) |
| | 16 | |
| | 17 | iterateeI2E :: (Monoid s, Monad m) => I.Iteratee s m a -> E.Iteratee s m a |
| | 18 | iterateeI2E = CE.coroutineIteratee . CI.iterateeCoroutine |
| | 19 | |
| | 20 | iterateeE2I :: (Monoid s, Monad m) => E.Iteratee s m a -> I.Iteratee s m a |
| | 21 | iterateeE2I = CI.coroutineIteratee . CE.iterateeCoroutine |
| | 22 | enumeratorI2E :: (Monoid s, Monad m) => I.Enumerator s (Coroutine (Yield [s]) m) () -> E.Enumerator s m () |
| | 23 | enumeratorI2E = CE.coroutineEnumerator . CI.enumeratorCoroutine |
| | 24 | |
| | 25 | enumeratorE2I :: (Monoid s, Monad m) => E.Enumerator s (Coroutine (Yield [s]) m) () -> I.Enumerator s m () |
| | 26 | enumeratorE2I = CI.coroutineEnumerator . CE.enumeratorCoroutine |
| | 27 | }}} |
| | 28 | |
| | 29 | Here's an example that uses `enumInflate` from the (iteratee-based) package `iteratee-compress`, together with the |
| | 30 | enumerator-based function `iterHandle`, to build an iteratee that writes data into a compressed file: |
| | 31 | |
| | 32 | {{{ |
| | 33 | import Control.Monad ((>=>)) |
| | 34 | import System.IO (Handle, withBinaryFile, IOMode(WriteMode)) |
| | 35 | import Control.Monad.IO.Class (MonadIO(liftIO)) |
| | 36 | import Data.ByteString (ByteString) |
| | 37 | import Data.ByteString.Char8 (pack) |
| | 38 | import Data.Enumerator.IO (iterHandle) |
| | 39 | import Data.Iteratee.ZLib (enumDeflate, Format(GZip), defaultCompressParams) |
| | 40 | |
| | 41 | iterCompressHandle :: MonadIO m => Handle -> m (I.Iteratee ByteString m ()) |
| | 42 | iterCompressHandle = enumDeflate GZip defaultCompressParams . iterateeE2I . iterHandle |
| | 43 | |
| | 44 | main = withBinaryFile "hello.gz" WriteMode $ |
| | 45 | \h-> iterCompressHandle h |
| | 46 | >>= I.enumPure1Chunk (pack "Hello, World!\n") |
| | 47 | >>= foldr (>=>) return (replicate 10 $ I.enumPure1Chunk (pack "Salut, Monde!\n")) |
| | 48 | >>= I.run |
| | 49 | }}} |