Class: Enumerator::Memoizing

Inherits:
Enumerator show all
Defined in:
lib/enumerator/memoizing.rb

Instance Method Summary collapse

Methods inherited from Enumerator

#memoizing

Constructor Details

#initialize(obj, *args) ⇒ Memoizing

Returns a new instance of Memoizing.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/enumerator/memoizing.rb', line 8

def initialize(obj, *args)
  if obj.kind_of? Enumerator
    base_enumerator = obj
  else
    base_enumerator = Enumerator.new(obj, *args)
  end

  memoized_sequence = []

  super() do |yielder|
    memoized_sequence.each do |m|
      yielder << m
    end

    loop do
      x = base_enumerator.next
      memoized_sequence << x
      yielder << x
    end
  end
end