Anyone know of any generic implementation of a pipeline in ruby? That
is, one which takes a bunch of GoF Command objects, and strings them
together in the classical pipeline configuration.
Any advice on that?
I have a process with a lot of sequential processing, and think that a
pipeline of Command objects (not just Proc’s) might be the way to do
it.
The downsides of pipelines:
You can’t do selections (if) or sequences (loops). If you can
handle this, though, this becomes a virtue, as it keeps everything
crystal clear and simple. Witness the ever useful Unix command line
pipelines.
They don’t encapsulate very well, as each stage totally overwrites
the previous ones. I’m not sure if I should just accept this, or try
to use some modification (append only pipeline, etc.).
Anyone know of any generic implementation of a pipeline in ruby? That
is, one which takes a bunch of GoF Command objects, and strings them
together in the classical pipeline configuration.
Any advice on that?
Does Rake approach this?
Define each command as a task. Let Rake handle the chaining and
dependency logic.
–
James B.
?Design depends largely on constraints.?
? Charles Eames
You can’t do selections (if) or sequences (loops). If you can
handle this, though, this becomes a virtue, as it keeps everything
crystal clear and simple. Witness the ever useful Unix command line
pipelines.
They don’t encapsulate very well, as each stage totally overwrites
the previous ones. I’m not sure if I should just accept this, or try
to use some modification (append only pipeline, etc.).
Any experience or ideas on this appreciated.
There are several ways you can do pipelining. Do you want concurrency
with that? Does every stage have a single output the next stage wants
to
operate on? etc.
Probably the simplest thing you can do is to use #inject:
commands = [
lambda {|x| x * 2},
lambda {|x| 0 - x},
lambda {|x| x + 10},
]
You can’t do selections (if) or sequences (loops). If you can
handle this, though, this becomes a virtue, as it keeps everything
crystal clear and simple. Witness the ever useful Unix command line
pipelines.
They don’t encapsulate very well, as each stage totally overwrites
the previous ones. I’m not sure if I should just accept this, or try
to use some modification (append only pipeline, etc.).
Depending on the number of processors you might even run into stack size
limits which won’t happen with an #inject based solution (see my other
posting). (Ok, I know that I’m being obsessive about Enumerable#inject
it was love at first sight. :-)) )
Although this looks nice, when associating Unix pipes you might be
tempted
to expect those to execute concurrently which they don’t.
We can combine both solutions:
class ProcChain
def initialize(*procs) @chain = procs end
def |(proc) @chain << proc
self
end
def call(a) @chain.inject(a) {|x,pr| pr[x]}
end
alias [] call
end
class Proc
def |(proc)
ProcChain.new(self, proc)
end
end
You can’t do selections (if) or sequences (loops). If you can
handle this, though, this becomes a virtue, as it keeps everything
crystal clear and simple. Witness the ever useful Unix command line
pipelines.
They don’t encapsulate very well, as each stage totally overwrites
the previous ones. I’m not sure if I should just accept this, or try
to use some modification (append only pipeline, etc.).