Class: Lazily::Zipper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lazily/zipping.rb

Instance Method Summary collapse

Methods included from Enumerable

#[], #chunk, #collect, #compact, #concat, #drop, #drop_while, #flat_map, #flatten, #grep, #in_threads, #lazily, #lazy?, #prefetch, #reject, #select, #slice_before, #take, #take_while, #uniq, #zip

Methods included from Enumerable

#lazily

Constructor Details

#initialize(enumerables) ⇒ Zipper

Returns a new instance of Zipper.



25
26
27
# File 'lib/lazily/zipping.rb', line 25

def initialize(enumerables)
  @enumerables = enumerables
end

Instance Method Details

#eachObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lazily/zipping.rb', line 29

def each
  enumerators = @enumerables.map(&:to_enum)
  exhausted = {}
  while true
    chunk = enumerators.map do |enumerator|
      begin
        enumerator.next unless exhausted[enumerator]
      rescue StopIteration
        exhausted[enumerator] = true
        nil
      end
    end
    break if chunk.all?(&:nil?)
    yield chunk
  end
end