Module: Minx::Utils

Included in:
Minx
Defined in:
lib/minx/utils.rb

Instance Method Summary (collapse)

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.

Parameters:

  • input (Channel)

    input channel

  • output (Channel)

    output channel

Yield Parameters:

  • message

Returns:

  • (Process)

    the filtering process

Raises:

  • (ArgumentError)

    if no block is given



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 |message|
      if yield message
        output.write(message)
      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 }

Parameters:

  • input (Channel)

    input channel

  • output (Channel)

    output channel

Yield Parameters:

  • message

Returns:

  • (Process)

    the mapping process

Raises:

  • (ArgumentError)

    if no block is given



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 |message|
      output.write(yield message)
    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

Yield Parameters:

  • channel (Channel)

    the communication channel

Returns:

  • (Channel)

    the channel being produced on.



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