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__.



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

def __memo__
  @__memo__
end

#__memo_instance__Object

Returns the value of attribute memo_instance.



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

def __memo_instance__
  @__memo_instance__
end

Class Method Details

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

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



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

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



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

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