Sha256: 14690e3044f0d7d1cccb96a3aa1db72aca0116dd89f8a575662c33e3992e4570

Contents?: true

Size: 823 Bytes

Versions: 13

Compression:

Stored size: 823 Bytes

Contents

class Enumerator
  # @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
  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

  attr_accessor :__memo__, :__memo_instance__

  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
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
abstractivator-0.16.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.15.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.14.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.13.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.12.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.11.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.10.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.8.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.7.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.6.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.5.0 lib/abstractivator/enumerator_ext.rb
abstractivator-0.4.1 lib/abstractivator/enumerator_ext.rb
abstractivator-0.4.0 lib/abstractivator/enumerator_ext.rb