本文介绍了在管道中运行消费者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个消费者和一个管道,以便消费者的输出可以输入管道的输入。



我想这可以通过combinator像这样:

 消费者imr  - > (r  - > Producer o m r') - > Pipe iom r'

或者这个:

  Consumer im i' - > Pipe i'o m r  - > Pipe iomr 

或像下面这样的提升函数:

 消费者imr  - > Pipe iomr 

或者像这样:

 消费者imo  - > Pipe iomr 

我试着做消费者>〜pipe Pipe iomr 可以这样做:

  { - #LANGUAGE RankNTypes# - } 
导入管道

foo :: Monad m =>消费者'我m o - > Pipe iom()
foo consumer = consumer>> = yield

Consumer'多态类型同义词,由于它不是真正关闭的下游,它可以用作 Pipe 从来没有真正 yield s。为了让它产生消费者的回报价值,我们简单地使用monadic绑定。



至于你的 Consumer i m r - >管道iomr 签名,它只是使用多态类型同义词的标识:

  iden :: Monad m =>消费者'我 -   - > Pipe i o m r 
iden consumer = consumer


I need to compose a consumer and a pipe so that the output of the consumer would feed the input of the pipe.

I guess this could be solved with a combinator like this:

Consumer i m r -> (r -> Producer o m r') -> Pipe i o m r'

or this:

Consumer i m i' -> Pipe i' o m r -> Pipe i o m r

or a lifting function like the following:

Consumer i m r -> Pipe i o m r

or like this:

Consumer i m o -> Pipe i o m r

I tried doing consumer >~ pipe without a success. So how to approach this?

解决方案

Something similar to your signature Consumer i m o -> Pipe i o m r could be done like this:

{-# LANGUAGE RankNTypes #-}
import Pipes

foo :: Monad m => Consumer' i m o -> Pipe i o m ()
foo consumer = consumer >>= yield

I have used the Consumer' polymorphic type synonym which, since it is not really closed "downstream", it can be used as a Pipe that never actually yields. To make it yield the return value of the consumer, we simply use a monadic bind.

As for your Consumer i m r -> Pipe i o m r signature, it's simply the identity using the polymorphic type synonym:

iden :: Monad m => Consumer' i m r -> Pipe i o m r
iden consumer = consumer

这篇关于在管道中运行消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:21