Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a pipe that consumes all of the producer output and passes it on as a list?

Tags:

haskell

I'm trying to use the Pipes library to model a workflow. In that workflow, I would like to accumulate all of that output from a producer, and then pass that on. In this case, I know that my producer produces a finite amount of output.

So if I have:

prod :: Producer a m ()
accum :: Pipe a [a] m r
groupConsumer :: Consumer [a] m r

how do I model accum so that I can do:

runEffect $ prod >-> accum >-> groupConsumer

Thanks!

like image 637
Tetigi Avatar asked Jan 06 '14 11:01

Tetigi


People also ask

What is a limitation of a pipe?

A limitation of pipes for interprocess communication is that the processes using pipes must have a common parent process (that is, share a common open or initiation process and exist as the result of a fork system call from a parent process).

How do you pipe the output of a command to another command?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do you pass the output of a command as an argument?

In this case, the output of the process and command substitution provided by parallel is being used as arguments to the cp command. We use the tokens “::::” and “:::” since parallel uses what is on the right as a file-argument and argument respectively.

How much data can a pipe hold?

Since Linux 2.6. 11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a system with a page size of 4096 bytes).


1 Answers

You can use Pipes.Prelude.toListM to collect a Producer into a list:

Pipes.Prelude.toListM :: (Monad m) => Producer a m () -> m [a]

Pipes.Prelude.toListM prod :: (Monad m) => m [a]

Then you just feed that list to your groupConsumer:

runEffect $ (lift (Pipes.Prelude.toListM prod) >>= yield) >-> groupConsumer
like image 55
Gabriella Gonzalez Avatar answered Oct 07 '22 06:10

Gabriella Gonzalez