module Enumerators
Private Instance Methods
character_enumerator(string)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 39 def character_enumerator(string) Enumerator.new do |y| index = 0 loop do raise StopIteration.new unless index < string.size y << string[index] index = index + 1 end end.lazy end
cycle_enumerator(e)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 75 def cycle_enumerator(e) Enumerator.new do |y| loop do e.rewind unless has_next(e) y << e.next end end.lazy end
empty_enumerator()
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 84 def empty_enumerator [].lazy end
enumerator_of(fn, init)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 16 def enumerator_of(fn, init) Enumerator.new do |y| value = init y << value loop do value = fn.(value) y << value end end.lazy end
flatten_enumerator(enumerator)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 50 def flatten_enumerator(enumerator) enumerator.rewind Enumerator.new do |y| current_enumerator = empty_enumerator get_current_enumerator = ->() { until has_next(current_enumerator) return empty_enumerator unless has_next(enumerator) current_enumerator = enumerator.next.enumerator current_enumerator.rewind end current_enumerator } loop do current = get_current_enumerator.() if has_next(current) y << current.next else raise StopIteration.new end end end.lazy end
has_next(e)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 7 def has_next(e) begin e.peek true rescue StopIteration false end end
repeat_enumerator(value)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 35 def repeat_enumerator(value) repeat_fn_enumerator(returns(value)) end
repeat_fn_enumerator(fn)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 27 def repeat_fn_enumerator(fn) Enumerator.new do |y| loop do y << fn.() end end.lazy end
reverse_enumerator(e)
click to toggle source
# File lib/totally_lazy/enumerators.rb, line 3 def reverse_enumerator(e) e.reverse_each end