Class: Enumerator
Instance Attribute Summary collapse
-
#__memo__ ⇒ Object
Returns the value of attribute __memo__.
-
#__memo_instance__ ⇒ Object
Returns the value of attribute memo_instance.
Class Method Summary collapse
-
.unfold(state) {|state| ... } ⇒ Object
Enumerate values produced from an initial state.
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.
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
#memoized ⇒ Object
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 |