Module: Enumerable

Defined in:
lib/slugforge/helper/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#ordered_parallel_mapObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/slugforge/helper/enumerable.rb', line 32

def ordered_parallel_map
  queue = Queue.new

  self.map.with_index do |item, index|
    Thread.new do
      # NOTE: You can not do anything that is not thread safe in this block...
      queue << [index, yield(item)]
    end
  end.each(&:join)

  [].tap do |results|
    results << queue.pop until queue.empty?
  end.sort.map {|index, item| item }
end

#parallel_mapObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/slugforge/helper/enumerable.rb', line 2

def parallel_map
  queue = Queue.new

  self.map do |item|
    Thread.new do
      # NOTE: You can not do anything that is not thread safe in this block...
      queue << yield(item)
    end
  end.each(&:join)

  [].tap do |results|
    results << queue.pop until queue.empty?
  end
end

#parallel_map_with_indexObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slugforge/helper/enumerable.rb', line 17

def parallel_map_with_index
  queue = Queue.new

  self.map.with_index do |item, index|
    Thread.new do
      # NOTE: You can not do anything that is not thread safe in this block...
      queue << yield(item, index)
    end
  end.each(&:join)

  [].tap do |results|
    results << queue.pop until queue.empty?
  end
end