Module: Minx::Utils
- Included in:
- Minx
- Defined in:
- lib/minx/utils.rb
Instance Method Summary (collapse)
-
- (Process) filter(input, output) {|message| ... }
Filter messages from input.
-
- (Process) map(input, output) {|message| ... }
Map messages from input to output.
-
- (Channel) producer {|channel| ... }
Spawn a producer process attached to a channel.
Instance Method Details
- (Process) filter(input, output) {|message| ... }
Filter messages from input.
Yields messages read from input to the block argument, forwarding those for which the block returns true to the output channel.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/minx/utils.rb', line 42 def filter(input, output) unless block_given? raise ArgumentError.new("Please provide a block argument") end Minx.spawn do input.each do || if yield output.write() end end end end |
- (Process) map(input, output) {|message| ... }
Map messages from input to output.
Provide a block that takes a single argument, and it will be called with every message read from input. The result of the block will be written to output.
# c1 and c2 are channels.
double = Minx.map(c1, c2) {|i| i * 2 }
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/minx/utils.rb', line 19 def map(input, output) unless block_given? raise ArgumentError.new("Please provide a block argument") end Minx.spawn do input.each do || output.write(yield ) end end end |
- (Channel) producer {|channel| ... }
Spawn a producer process attached to a channel.
fib = Minx.producer do |chan|
i, j = 0, 1
loop do
chan << i
i, j = j, i + j
end
end
fib.read #=> 0
fib.read #=> 1
fib.read #=> 1
fib.read #=> 2
73 74 75 76 77 |
# File 'lib/minx/utils.rb', line 73 def producer(&block) chan = Minx.channel Minx.spawn { block.call(chan) } return chan end |