Parallel iterators and streams without threads - Call to experts
After trial and error I found a way to iterate over two streams in parallel.
Being new to Ruby it was an useful exercise in forcing me to look intensely
at a lot of features. The solution below works, but I have three isuues.
1. WHile I played with different forms of 'callcc' and finally arrived at
the statement shown inidicated by ???? in method 'second' I am puzzled by
its
semantics and why it works? A clear explanation owuld be valuable.'
2. The solution is not quite symmetric
3. I would like the 'second' method itself to achieve closure by also
behaving as an iterator. The idea would be
4. Conceptually the way I would like to think about and express it would be
as follows
- the parentheses may be omitted since the notion of paralel assignment is
already there in
Ruby. I think it would be nice t have a feature like this. It would make
stream programming
a lot easier in Ruby. Icould implement data flow programming rather
straightforwardly.
The solution also separates usage of foo and bar from the method themselves.
Curious to hear from the Ruby experts
(foo,bar) do |i,j|
#process i and j as needed
yield i,j # to achieve closure
end
-------------------------------------------
def test
x=second
foo { |i| x= callcc {|cc| x.call cc, i }}
end
def second()
test1, i = callcc { |cc| return cc } # ????
bar do |j|
print "#{i},#{j}\n"
test1,i = callcc { |cc| test1.call cc }
end
end
#stream 1
def foo
for i in 1..4
yield i
end
end
#stream 2
def bar
for i in 5.. 8
yield i
end
end
test
Output-
1,5
2,6
3,7
4,8 matz.
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com