Changes between Version 1 and Version 2 of coroutine-iteratee

Show
Ignore:
Timestamp:
11/28/10 19:22:04 (4 years ago)
Author:
blamario (IP: 99.241.117.248)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • coroutine-iteratee

    v1 v2  
    11= coroutine-iteratee = 
    22 
    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. 
     3The 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 
     6combined into a bridge between the existing enumerator-based and iteratee-based libraries: 
     7 
     8{{{ 
     9import Data.Monoid (Monoid) 
     10import qualified Data.Enumerator as E 
     11import qualified Data.Iteratee as I 
     12import qualified Control.Monad.Coroutine.Enumerator as CE 
     13import qualified Control.Monad.Coroutine.Iteratee as CI 
     14import Control.Monad.Coroutine (Coroutine) 
     15import Control.Monad.Coroutine.SuspensionFunctors (Yield) 
     16 
     17iterateeI2E :: (Monoid s, Monad m) => I.Iteratee s m a -> E.Iteratee s m a 
     18iterateeI2E = CE.coroutineIteratee . CI.iterateeCoroutine 
     19 
     20iterateeE2I :: (Monoid s, Monad m) => E.Iteratee s m a -> I.Iteratee s m a 
     21iterateeE2I = CI.coroutineIteratee . CE.iterateeCoroutine 
     22enumeratorI2E :: (Monoid s, Monad m) => I.Enumerator s (Coroutine (Yield [s]) m) () -> E.Enumerator s m () 
     23enumeratorI2E = CE.coroutineEnumerator . CI.enumeratorCoroutine 
     24 
     25enumeratorE2I :: (Monoid s, Monad m) => E.Enumerator s (Coroutine (Yield [s]) m) () -> I.Enumerator s m () 
     26enumeratorE2I = CI.coroutineEnumerator . CE.enumeratorCoroutine 
     27}}} 
     28 
     29    Here's an example that uses `enumInflate` from the (iteratee-based) package `iteratee-compress`, together with the 
     30enumerator-based function `iterHandle`, to build an iteratee that writes data into a compressed file: 
     31 
     32{{{ 
     33import Control.Monad ((>=>)) 
     34import System.IO (Handle, withBinaryFile, IOMode(WriteMode)) 
     35import Control.Monad.IO.Class (MonadIO(liftIO)) 
     36import Data.ByteString (ByteString) 
     37import Data.ByteString.Char8 (pack) 
     38import Data.Enumerator.IO (iterHandle) 
     39import Data.Iteratee.ZLib (enumDeflate, Format(GZip), defaultCompressParams) 
     40 
     41iterCompressHandle :: MonadIO m => Handle -> m (I.Iteratee ByteString m ()) 
     42iterCompressHandle = enumDeflate GZip defaultCompressParams . iterateeE2I . iterHandle 
     43 
     44main = 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}}}