Module: Enumerable

Defined in:
lib/vendor/parallel/parallel.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#parallel_all?(n, &block) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/vendor/parallel/parallel.rb', line 81

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)


89
90
91
92
93
94
95
# File 'lib/vendor/parallel/parallel.rb', line 89

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



17
18
19
20
21
22
23
24
# File 'lib/vendor/parallel/parallel.rb', line 17

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



73
74
75
76
77
78
79
# File 'lib/vendor/parallel/parallel.rb', line 73

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)


97
98
99
100
101
102
103
# File 'lib/vendor/parallel/parallel.rb', line 97

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



26
27
28
29
30
31
32
# File 'lib/vendor/parallel/parallel.rb', line 26

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



50
51
52
53
54
# File 'lib/vendor/parallel/parallel.rb', line 50

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



56
57
58
59
60
# File 'lib/vendor/parallel/parallel.rb', line 56

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/parallel/parallel.rb', line 62

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



42
43
44
45
46
47
48
# File 'lib/vendor/parallel/parallel.rb', line 42

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



34
35
36
37
38
39
40
# File 'lib/vendor/parallel/parallel.rb', line 34

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



105
106
107
# File 'lib/vendor/parallel/parallel.rb', line 105

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