Class: Enumerator

Inherits:
Object show all
Defined in:
lib/core/facets/enumerator.rb,
lib/core/facets/enumerator/fx.rb,
lib/core/facets/enumerator/lazy/squeeze.rb

Direct Known Subclasses

Denumerator

Defined Under Namespace

Classes: Lazy, Yielder

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Enumerator (private)

Provides the ruby-1.9 block form of Enumerator, where you can write:

obj = Enumerator.new do |yielder|
  # ...
  yielder.yield(data)  # or: yielder << data
  # ...
end

When obj.each is called, the block is run once. It should call yielder.yield with each item it wishes to generate.

Example:

fib = Enumerator.new { |y|
  a = b = 1
  loop {   
    y << a
    a, b = b, a + b
  }
}  

fib.take(10)  #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]


37
38
39
40
41
42
43
44
# File 'lib/core/facets/enumerator.rb', line 37

def initialize(*args, &block)
  if block
    @body = block
    old_initialize(self, :_start)
  else
    old_initialize(*args)
  end
end

Instance Method Details

#_start(*args, &receiver) ⇒ Object (private)

:nodoc:



46
47
48
# File 'lib/core/facets/enumerator.rb', line 46

def _start(*args,&receiver) #:nodoc:
  @body.call(Yielder.new(&receiver), *args)
end

#fxObject



8
9
10
11
12
# File 'lib/core/facets/enumerator/fx.rb', line 8

def fx
  Functor.new do |op, *a, &b|
    map{ |e| e.send(op, *a, &b) }
  end
end

#old_initializeObject (private)



12
# File 'lib/core/facets/enumerator.rb', line 12

alias :old_initialize :initialize