Class Coroutine
In: lib/more/facets/coroutine.rb
Parent: Object

Coroutine

Coroutines are program components like subroutines. Coroutines are more generic and flexible than subroutines, but are less widely used in practice. Coroutines were first introduced natively in Simula. Coroutines are well suited for implementing more familiar program components such as cooperative tasks, iterators, infinite lists, and pipes.

This mixin solely depends on method read(n), which must be defined in the class/module where you mix in this module.

Usage

  count = (ARGV.shift || 1000).to_i
  input = (1..count).map { (rand * 10000).round.to_f / 100}

  Producer = Coroutine.new do |me|
    loop do
      1.upto(6) do
        me[:last_input] = input.shift
        me.resume(Printer)
      end
      input.shift # discard every seventh input number
    end
  end
  Printer = Coroutine.new do |me|
    loop do
      1.upto(8) do
        me.resume(Producer)
        if Producer[:last_input]
          print Producer[:last_input], "\t"
          Producer[:last_input] = nil
        end
        me.resume(Controller)
      end
      puts
    end
  end

  Controller = Coroutine.new do |me|
    until input.empty? do
      me.resume(Printer)
    end
  end

  Controller.run

Methods

[]   []=   continue   new   resume   run   stop  

Attributes

stopped  [R] 

Public Class methods

[Source]

# File lib/more/facets/coroutine.rb, line 115
  def initialize(data = {})
    @stopped = nil
    @data = data
    callcc do |@continue|
      return
    end
    yield self
    stop
  end

Public Instance methods

[Source]

# File lib/more/facets/coroutine.rb, line 148
  def [](name)
    @data[name]
  end

[Source]

# File lib/more/facets/coroutine.rb, line 152
  def []=(name, value)
    @data[name] = value
  end

[Source]

# File lib/more/facets/coroutine.rb, line 137
  def resume(other)
    callcc do |@continue|
      other.continue(self)
    end
  end

[Source]

# File lib/more/facets/coroutine.rb, line 127
  def run
    callcc do |@stopped|
      continue
    end
  end

[Source]

# File lib/more/facets/coroutine.rb, line 133
  def stop
    @stopped.call
  end

Protected Instance methods

[Source]

# File lib/more/facets/coroutine.rb, line 143
  def continue(from = nil)
    @stopped = from.stopped if not @stopped and from
    @continue.call
  end

[Validate]