Module: SlowEnumeratorTools::Batcher

Defined in:
lib/slow_enumerator_tools/batcher.rb

Class Method Summary collapse

Class Method Details

.batch(enum) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/slow_enumerator_tools/batcher.rb', line 5

def self.batch(enum)
  q = Queue.new
  stop = Object.new

  t =
    Thread.new do
      enum.each do |e|
        q << e
      end
      q << stop
    end

  Enumerator.new do |y|
    loop do
      res = []
      ended = false

      # pop first
      elem = q.pop
      break if stop.equal?(elem)
      res << elem

      loop do
        # pop remaining
        begin
          elem = q.pop(true)
        rescue ThreadError
          break
        end
        if stop.equal?(elem)
          ended = true
          break
        end
        res << elem
      end

      y << res
      break if ended
    end

    t.join
  end.lazy
end