Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering / branching enumeratee
    primarykey
    data
    text
    <p>I am using enumerator-0.4.10, and I need to distribute processing of different parts of the incoming stream to different iteratees (I am parsing a huge XML file, and different sub-trees have different processing logic). Only a single iteratee will be active at a time since the sub-trees don't intersect.</p> <p>I wrote a simple example that filters the stream and passes the result to one iteratee; please see below. However, with multiple nested iteratees it seems to me that I can no longer use an enumeratee. Do I need to write my own multi-enumeratee that holds multiple inner iteratees? Any better ideas?</p> <p>Here is my (beginner's) code for a single nested iteratee:</p> <pre><code>module Main ( main ) where import qualified Data.Enumerator as E ( Enumeratee, Step(..), Stream(..), checkDone, checkDoneEx, continue, enumList, joinI, run_, yield ) import Data.Enumerator ( ($$), (&gt;&gt;==) ) import qualified Data.Enumerator.List as EL ( consume ) -- cribbed from EL.concatMap concatMapAccum :: Monad m =&gt; (s -&gt; ao -&gt; (s, [ai])) -&gt; s -&gt; E.Enumeratee ao ai m b concatMapAccum f s0 = E.checkDone (E.continue . step s0) where step _ k E.EOF = E.yield (E.Continue k) E.EOF step s k (E.Chunks xs) = loop s k xs loop s k [] = E.continue (step s k) loop s k (x:xs) = case f s x of (s', ais) -&gt; k (E.Chunks $ ais) &gt;&gt;== E.checkDoneEx (E.Chunks xs) (\k' -&gt; loop s' k' xs) passFromTo :: Monad m =&gt; ((a -&gt; Bool), (a -&gt; Bool)) -&gt; Bool -&gt; E.Enumeratee a a m b passFromTo (from, to) pass0 = concatMapAccum updatePass pass0 where updatePass pass el = case (pass, from el, to el) of (True, _, to_el) -&gt; (not to_el, [el]) (False, True, _) -&gt; (True, [el]) (False, False, _) -&gt; (False, []) main :: IO() main = do E.run_ (E.enumList 3 [1..20] $$ E.joinI $ passFromTo ((\e -&gt; e == 3 || e == 13), (\e -&gt; e == 7 || e == 17)) False $$ EL.consume) &gt;&gt;= print $ ./dist/build/StatefulEnumeratee/StatefulEnumeratee [3,4,5,6,7,13,14,15,16,17] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload