Class: Enumerator

Inherits:
Object show all
Defined in:
lib/abstractivator/enumerator_ext.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__memo__Object

Returns the value of attribute __memo__.



19
20
21
# File 'lib/abstractivator/enumerator_ext.rb', line 19

def __memo__
  @__memo__
end

#__memo_instance__Object

Returns the value of attribute memo_instance.



19
20
21
# File 'lib/abstractivator/enumerator_ext.rb', line 19

def __memo_instance__
  @__memo_instance__
end

Class Method Details

.unfold(state) {|state| ... } ⇒ Object

Enumerate values produced from an initial state. The enumerator terminates when the next state is nil.

Parameters:

  • state (Object)

    The initial state

Yield Parameters:

  • state (Object)

    the current state

Yield Returns:

  • (Array)

    a 2-element array containing the next value and the next state



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/abstractivator/enumerator_ext.rb', line 6

def self.unfold(state)
  raise 'block is required' unless block_given?
  Enumerator.new do |y|
    unless state.nil?
      loop do
        next_value, state = yield(state)
        break if state.nil?
        y << next_value
      end
    end
  end
end

Instance Method Details

#memoizedObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/abstractivator/enumerator_ext.rb', line 21

def memoized
  @__memo_instance__ ||= self.dup
  inner = __memo_instance__
  inner.__memo__ ||= []
  Enumerator.new do |y|
    i = 0
    loop do
      inner.__memo__ << inner.next while inner.__memo__.size <= i
      y << inner.__memo__[i]
      i += 1
    end
  end
end