Changes between Initial Version and Version 1 of coroutine-enumerator

Show
Ignore:
Timestamp:
11/13/10 17:54:23 (4 years ago)
Author:
blamario (IP: 99.241.117.248)
Comment:

From the coroutine-enumerator package announcement message

Legend:

Unmodified
Added
Removed
Modified
  • coroutine-enumerator

    v1 v1  
     1= coroutine-enumerator = 
     2 
     3The coroutine-enumerator package can be used as a bridge between the [http://hackage.haskell.org/package/enumerator enumerator] and [wiki:monad-coroutine] packages. It provides two-way conversion functions between an Iteratee and an Await-suspending coroutine, and also 
     4between an Enumerator and a Yield-suspending coroutine. 
     5 
     6As a little example, the following program combines the http-enumerator, monad-coroutine, and SCC packages using the 
     7coroutine-enumerator package to print out all lines from the Hackage database containing substring "enumerator": 
     8 
     9{{{ 
     10import Control.Exception.Base (SomeException) 
     11import Control.Monad.Trans.Class (lift) 
     12 
     13import Data.ByteString (ByteString) 
     14import Data.Text.Encoding (decodeUtf8) 
     15 
     16import Network.HTTP.Enumerator 
     17 
     18import Control.Monad.Coroutine 
     19import Control.Monad.Coroutine.SuspensionFunctors 
     20import Control.Monad.Coroutine.Nested 
     21import Control.Monad.Coroutine.Enumerator 
     22 
     23import Control.Concurrent.SCC.Sequential 
     24 
     25main = httpRedirect (\_ _-> coroutineIteratee consumer) =<< parseUrl address 
     26 
     27address = "http://hackage.haskell.org/packages/archive/pkg-list.html" 
     28 
     29consumer :: Coroutine (Await [ByteString]) IO (Either SomeException ((), [ByteString])) 
     30consumer = pipe translator (consume worker) >> return (Right ((), [])) 
     31 
     32translator :: Functor f => Sink IO (EitherFunctor (Await [a]) f) a -> Coroutine (EitherFunctor (Await [a]) f) IO () 
     33translator sink = do chunks <- liftParent await 
     34                     if null chunks 
     35                        then lift (putStrLn "END") 
     36                        else putList chunks sink >> translator sink 
     37          
     38worker :: Consumer IO ByteString () 
     39worker = toChars >-> foreach (line `having` substring "enumerator") 
     40                             (append (fromList "\n") >-> toStdOut) 
     41                             suppress 
     42 
     43toChars :: Monad m => Transducer m ByteString Char 
     44toChars = oneToOneTransducer decodeUtf8 >-> coerce 
     45}}} 
     46 
     47 
     48Alternatively, the worker coroutine can parse the XML database and print out all elements whose any attribute value 
     49contains the substring "enumerator": 
     50 
     51{{{ 
     52worker = toChars 
     53         >-> parseXMLTokens  
     54         >-> foreach (xmlElementHavingTagWith (xmlAttributeValue `having` substring "enumerator") 
     55                      `nestedIn` xmlElementContent) 
     56                     (coerce >-> toStdOut)  
     57                     suppress 
     58}}}