将两个消费者加入到返回多个值的单个消费者中? [英] Join two consumers into a single consumer that returns multiple values?

查看:108
本文介绍了将两个消费者加入到返回多个值的单个消费者中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验新的管道-http包,我有一个想法。我有两个网页解析器,一个返回页面中的行项目和另一个数字。当我抓取页面时,将这些解析器串在一起并同时从相同的字符串生成器获取它们的结果是非常好的,而不是获取页面两次或者将所有html读入内存并解析它两次。

  c1 :: p 

消费者是r1
c2 ::消费者是r2

是否有可能使某个功能这:

  combineConsumers :: Consumer am r1  - >消费者a m r2  - >消费者am(r1,r2)
combineConsumers =未定义

我尝试了几件事情,但我无法弄清楚。



编辑:



我是对不起,事实证明我正在做一个关于pipes-attoparsec的假设,由于我对管道attoparsec的经验,导致我提出错误的问题。当我假设它会返回一个管道消费者时,Pipes-attoparsec将一个attoparsec变成一个管道解析器。这意味着我实际上不能将两个attoparsec解析器转换为接受文本并返回结果的消费者,然后将它们与普通的旧管道生态系统一起使用。我很抱歉,但我只是不明白pipes-parse。



即使它没有帮助我,但Arthur的答案与我想象的几乎一样这个问题,我可能最终会在未来使用他的解决方案。与此同时,我只是想使用管道。

解决方案

我认为你对这种方式有所不同,这是达沃拉克在他的评论中提到的原因。但是如果你真的需要这样一个功能,你可以定义它。

 导入Pipes.Internal 
导入Pipes.Core

zipConsumers :: Monad m = >消费者a m r - >消费者a - s - >消费者am(r,s)
zipConsumers pq = go(p,q)其中
go(p,q)=
(Pure r,Pure s) - > Pure(r,s)
(M mpr,ps) - > M(do pr < - mpr
return(go(pr,ps)))
(pr,M mps) - > M(do ps < - mps
return(go(pr,ps)))
(Request_f,Request_g) - > Request()(\ a→go(f a,g a))
(Request _f,Pure s) - > Request()(\ a→> do r< -f a
return(r,s))
(Pure r,Request_g) - > Request()(\ a - > do s < - g a
return(r,s))
(Respond x _,_) - >已关闭x
(_,回应y _) - >已关闭y

如果您在不使用返回值的情况下拉近消费者,只有他们的效果可以使用 tee consumer1> - > consumer2


I have been experimenting with the new pipes-http package and I had a thought. I have two parsers for a web page, one that returns line items and another a number from elsewhere in the page. When I grab the page, it'd be nice to string these parsers together and get their results at the same time from the same bytestring producer, rather than fetching the page twice or fetching all the html into memory and parsing it twice.

In other words, say you have two Consumers:

c1 :: Consumer a m r1
c2 :: Consumer a m r2

Is it possible to make a function like this:

combineConsumers :: Consumer a m r1 -> Consumer a m r2 -> Consumer a m (r1, r2)
combineConsumers = undefined

I have tried a few things, but I can't figure it out. I understand if it isn't possible, but it would be convenient.

Edit:

I'm sorry it turns out I was making an assumption about pipes-attoparsec, due to my experience with conduit-attoparsec that caused me to ask the wrong question. Pipes-attoparsec turns an attoparsec into a pipes Parser when I just assumed that it would return a pipes Consumer. That means that I can't actually turn two attoparsec parsers into consumers that take text and return a result, then use them with the plain old pipes ecosystem. I'm sorry but I just don't understand pipes-parse.

Even though it doesn't help me, Arthur's answer is pretty much what I envisioned when I asked the question, and I'll probably end up using his solution in the future. In the meantime I'm just going to use conduit.

解决方案

I think something is wrong with the way you are going about this, for the reasons Davorak mentions in his remark. But if you really need such a function, you can define it.

import Pipes.Internal
import Pipes.Core

zipConsumers :: Monad m => Consumer a m r -> Consumer a m s -> Consumer a m (r,s)
zipConsumers p q = go (p,q) where
  go (p,q) = case (p,q) of 
     (Pure r     , Pure s)      -> Pure (r,s)
     (M mpr      , ps)          -> M (do pr <- mpr
                                         return (go (pr, ps)))
     (pr         , M mps)       -> M (do ps <- mps
                                         return (go (pr, ps)))
     (Request _ f, Request _ g) -> Request () (\a -> go (f a, g a))
     (Request _ f, Pure s)      -> Request () (\a -> do r <- f a
                                                        return (r, s))
     (Pure r     , Request _ g) -> Request () (\a -> do s <- g a
                                                        return (r,s))
     (Respond x _, _          ) -> closed x
     (_          , Respond y _) -> closed y

If you are 'zipping' consumers without using their return value, only their 'effects' you can just use tee consumer1 >-> consumer2

这篇关于将两个消费者加入到返回多个值的单个消费者中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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