class Enumerator

Attributes

__memo__[RW]
__memo_instance__[RW]

Public Class Methods

unfold(state) { |state| ... } click to toggle source

Enumerate values produced from an initial state. The enumerator terminates when the next state is nil. @param state [Object] The initial state @yieldparam state [Object] the current state @yieldreturn [Array] a 2-element array containing the next value and the next state

# 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

Public Instance Methods

memoized() click to toggle source
# 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