Module: Enumerable

Defined in:
lib/concurrent/parallel.rb

Instance Method Summary collapse

Instance Method Details

#parallel_all?(n, &block) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/concurrent/parallel.rb', line 79

def parallel_all?( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.all? &block }
  end.inject( true ) do |acc, thread|
    acc && thread.value
  end
end

#parallel_any?(n, &block) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/concurrent/parallel.rb', line 87

def parallel_any?( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.any? &block }
  end.inject( false ) do |acc, thread|
    acc || thread.value
  end
end

#parallel_each(n, &block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/concurrent/parallel.rb', line 15

def parallel_each( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.each &block }
  end.each do |thread|
    thread.join
  end
  self
end

#parallel_grep(re, n, &block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/concurrent/parallel.rb', line 71

def parallel_grep( re, n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.grep( re, &block ) }
  end.inject( [] ) do |acc, thread|
    acc.push *thread.value
  end
end

#parallel_include?(n, obj) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/concurrent/parallel.rb', line 95

def parallel_include?( n, obj )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.include? obj }
  end.inject( false ) do |acc, thread|
    acc || thread.value
  end
end

#parallel_map(n, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/concurrent/parallel.rb', line 24

def parallel_map( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.map &block }
  end.inject( [] ) do |a, thread|
    a.push *thread.value
  end
end

#parallel_max(n) ⇒ Object



48
49
50
51
52
# File 'lib/concurrent/parallel.rb', line 48

def parallel_max( n )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.max }
  end.map { |t| t.value }.max
end

#parallel_min(n) ⇒ Object



54
55
56
57
58
# File 'lib/concurrent/parallel.rb', line 54

def parallel_min( n )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.min }
  end.map { |t| t.value }.min
end

#parallel_partition(n, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/concurrent/parallel.rb', line 60

def parallel_partition( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.partition &block }
  end.inject( [ [], [] ] ) do |acc, thread|
    pair = thread.value
    acc[0].push *pair[0]
    acc[1].push *pair[1]
    acc
  end
end

#parallel_reject(n, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/concurrent/parallel.rb', line 40

def parallel_reject( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.reject &block }
  end.inject( [] ) do |a, thread|
    a.push *thread.value
  end
end

#parallel_select(n, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/concurrent/parallel.rb', line 32

def parallel_select( n, &block )
  parallel_subsets( n ).map do |slice|
    Thread.new { slice.select &block }
  end.inject( [] ) do |a, results|
    a.push *thread.value
  end
end

#parallel_subsets(n) ⇒ Object



103
104
105
# File 'lib/concurrent/parallel.rb', line 103

def parallel_subsets( n )
  to_a.parallel_subsets( n )
end