是否有类似于Iteratee的概念从多个来源提取数据? [英] Is there an Iteratee-like concept which pulls data from multiple sources?

查看:164
本文介绍了是否有类似于Iteratee的概念从多个来源提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用流(懒惰列表)可以从一个数字(比如说为了简单起见)拉动数据源的需求。迭代器可用于处理来自单一来源的数据。

It is possible to pull on demand from a number (say two for simplicity) of sources using streams (lazy lists). Iteratees can be used to process data coming from a single source.

是否有类似于Iteratee的功能概念用于处理多个输入源?我可以想象一个Iteratee,它的状态信号表明它想从哪个源拉取。

Is there an Iteratee-like functional concept for processing multiple input sources? I could imagine an Iteratee whose state signals from which source does it want to pull.

推荐答案

管道单体变压器本身,一旦你想与每个生产者进行交互。例如:

To do this using pipes you nest the Pipe monad transformer within itself, once for each producer you wish to interact with. For example:

import Control.Monad
import Control.Monad.Trans
import Control.Pipe

producerA, producerB :: (Monad m) => Producer Int m ()
producerA = mapM_ yield [1,2,3]
producerB = mapM_ yield [4,5,6]

consumes2 :: (Show a, Show b) =>
    Consumer a (Consumer b IO) r
consumes2 = forever $ do
    a <- await       -- await from outer producer
    b <- lift await  -- await from inner producer
    lift $ lift $ print (a, b)

就像Haskell curried函数

Just like a Haskell curried function of multiple variables, you partially apply it to each source using composition and runPipe:

consumes1 :: (Show b) => Consumer b IO ()
consumes1 = runPipe $ consumes2 <+< producerA

fullyApplied :: IO ()
fullyApplied = runPipe $ consumes1 <+< producerB

上面的函数在运行时输出:

The above function outputs when run:

>>> fullyApplied
(1, 4)
(2, 5)
(3, 6)

这个技巧适用于产生或等待任何数量的管道上游或下游。它也适用于代理,双向类似于管道。

This trick works for yielding or awaiting to any number of pipes upstream or downstream. It also works for proxies, the bidirectional analogs to pipes.

编辑:请注意,这也适用于任何迭代库,而不仅仅是 pipes 。事实上,John Milikin和Oleg是这种方法的原始倡导者,我只是从他们那里偷走了这个想法。

Note that this also works for any iteratee library, not just pipes. In fact, John Milikin and Oleg were the original advocates for this approach and I just stole the idea from them.

这篇关于是否有类似于Iteratee的概念从多个来源提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆