The aim is to emulate the very handy Go construct:
for i := range(c1) {
// Do something with i
}
# Channel Iterator
def channel_each (c1, f)
loop do
if i = c1.receive?
f.call(i)
else
break
end
end
end
c1 = Channel(Int32).new
c2 = Channel(Nil).new
# sender
spawn do
(1..10).each do |i|
sleep(100.milliseconds)
c1.send(i)
end
c1.close
c2.send nil
end
# receiver
spawn do
channel_each c1, ->(i : Int32) {
puts i
}
end
c2.receive
puts "Done"