我使用的是枚举器-0.4.10,我需要将处理的 传入流的不同部分分配给不同的迭代(我是 解析一个巨大的XML文件,并且不同的子树有不同的 处理逻辑)。由于子树不相交,因此只有一个迭代器在 时激活。过滤/分支枚举
我写了一个简单的例子,过滤流并将结果 传递给一个迭代器;请看下面。但是,对于多重嵌套的 迭代,我认为我不能再使用枚举器。 是否需要编写自己的多重枚举器来保存多个迭代器的内部 ?任何更好的想法?
下面是一个嵌套iteratee我(初级)代码:
module Main (main) where
import qualified Data.Enumerator as E (Enumeratee, Step(..), Stream(..),
checkDone, checkDoneEx, continue, enumList, joinI, run_, yield)
import Data.Enumerator (($$), (>>==))
import qualified Data.Enumerator.List as EL (consume)
-- cribbed from EL.concatMap
concatMapAccum :: Monad m => (s -> ao -> (s, [ai])) -> s ->
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) -> k (E.Chunks $ ais) >>==
E.checkDoneEx (E.Chunks xs) (\k' -> loop s' k' xs)
passFromTo :: Monad m => ((a -> Bool), (a -> Bool)) -> Bool -> 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) -> (not to_el, [el])
(False, True, _) -> (True, [el])
(False, False, _) -> (False, [])
main :: IO()
main = do
E.run_ (E.enumList 3 [1..20] $$
E.joinI $ passFromTo ((\e -> e == 3 || e == 13), (\e -> e == 7 || e == 17)) False $$
EL.consume) >>= print
$ ./dist/build/StatefulEnumeratee/StatefulEnumeratee
[3,4,5,6,7,13,14,15,16,17]