lite-ruby 1.0.14 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,227 +1,229 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Enumerable
3
+ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
4
+ module Enumerable
4
5
 
5
- def cluster
6
- each_with_object([]) do |ele, results|
7
- last_res = results.last
6
+ def cluster
7
+ each_with_object([]) do |ele, results|
8
+ last_res = results.last
8
9
 
9
- if last_res && (yield(ele) == yield(last_res.last))
10
- last_res << ele
10
+ if last_res && (yield(ele) == yield(last_res.last))
11
+ last_res << ele
12
+ else
13
+ results << [ele]
14
+ end
15
+ end
16
+ end
17
+
18
+ def cluster_by(&block)
19
+ group_by(&block).sort.transpose.pop || []
20
+ end
21
+
22
+ def deduce(identity = 0, &block)
23
+ if block_given?
24
+ map(&block).deduce(identity)
11
25
  else
12
- results << [ele]
26
+ inject { |acc, val| acc - val } || identity
13
27
  end
14
28
  end
15
- end
16
29
 
17
- def cluster_by(&block)
18
- group_by(&block).sort.transpose.pop || []
19
- end
30
+ def drop_last(num)
31
+ collection_size = to_a.size
32
+ return self if num > collection_size
20
33
 
21
- def deduce(identity = 0, &block)
22
- if block_given?
23
- map(&block).deduce(identity)
24
- else
25
- inject { |acc, val| acc - val } || identity
34
+ self[0...(collection_size - num)]
26
35
  end
27
- end
28
36
 
29
- def drop_last(num)
30
- collection_size = to_a.size
31
- return self if num > collection_size
37
+ def drop_last_if
38
+ dropping = true
39
+ reverse_each.with_object([]) do |val, arr|
40
+ next if dropping &&= yield(val)
32
41
 
33
- self[0...(collection_size - num)]
34
- end
42
+ arr.unshift(val)
43
+ end
44
+ end
35
45
 
36
- def drop_last_if
37
- dropping = true
38
- reverse_each.with_object([]) do |val, arr|
39
- next if dropping &&= yield(val)
46
+ def exactly?(num)
47
+ found_count = 0
40
48
 
41
- arr.unshift(val)
49
+ if block_given?
50
+ each { |*opt| found_count += 1 if yield(*opt) }
51
+ else
52
+ each { |opt| found_count += 1 if opt }
53
+ end
54
+
55
+ found_count > num ? false : num == found_count
42
56
  end
43
- end
44
57
 
45
- def exactly?(num)
46
- found_count = 0
58
+ # rubocop:disable Style/CaseEquality
59
+ def excase?(object)
60
+ none? { |val| object === val }
61
+ end
62
+ # rubocop:enable Style/CaseEquality
47
63
 
48
- if block_given?
49
- each { |*opt| found_count += 1 if yield(*opt) }
50
- else
51
- each { |opt| found_count += 1 if opt }
64
+ def exclude?(object)
65
+ !include?(object)
52
66
  end
53
67
 
54
- found_count > num ? false : num == found_count
55
- end
68
+ def expand
69
+ map { |val| val.is_a?(Enumerable) ? val.expand : val }
70
+ end
56
71
 
57
- # rubocop:disable Style/CaseEquality
58
- def excase?(object)
59
- none? { |val| object === val }
60
- end
61
- # rubocop:enable Style/CaseEquality
72
+ def exponential(identity = 0, &block)
73
+ if block_given?
74
+ map(&block).exponential(identity)
75
+ else
76
+ inject { |acc, val| acc**val } || identity
77
+ end
78
+ end
62
79
 
63
- def exclude?(object)
64
- !include?(object)
65
- end
80
+ def frequency
81
+ each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
82
+ end
66
83
 
67
- def expand
68
- map { |val| val.is_a?(Enumerable) ? val.expand : val }
69
- end
84
+ alias occurrences frequency
70
85
 
71
- def exponential(identity = 0, &block)
72
- if block_given?
73
- map(&block).exponential(identity)
74
- else
75
- inject { |acc, val| acc**val } || identity
86
+ # rubocop:disable Style/CaseEquality
87
+ def incase?(object)
88
+ any? { |val| object === val }
76
89
  end
77
- end
90
+ # rubocop:enable Style/CaseEquality
91
+
92
+ # rubocop:disable Metrics/MethodLength
93
+ def interpose(sep, &block)
94
+ enum = Enumerator.new do |val|
95
+ items = each
96
+
97
+ loop do
98
+ begin
99
+ val << items.next
100
+ rescue StopIteration
101
+ break
102
+ end
103
+
104
+ begin
105
+ items.peek
106
+ rescue StopIteration
107
+ break
108
+ else
109
+ val << sep
110
+ end
111
+ end
112
+ end
78
113
 
79
- def frequency
80
- each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
81
- end
114
+ block ? enum.each(&block) : enum
115
+ end
116
+ # rubocop:enable Metrics/MethodLength
82
117
 
83
- alias occurrences frequency
118
+ def many?
119
+ found_count = 0
84
120
 
85
- # rubocop:disable Style/CaseEquality
86
- def incase?(object)
87
- any? { |val| object === val }
88
- end
89
- # rubocop:enable Style/CaseEquality
90
-
91
- # rubocop:disable Metrics/MethodLength
92
- def interpose(sep, &block)
93
- enum = Enumerator.new do |val|
94
- items = each
95
-
96
- loop do
97
- begin
98
- val << items.next
99
- rescue StopIteration
100
- break
121
+ if block_given?
122
+ any? do |val|
123
+ found_count += 1 if yield(val)
124
+ found_count > 1
101
125
  end
126
+ else
127
+ any? { (found_count += 1) > 1 }
128
+ end
129
+ end
102
130
 
103
- begin
104
- items.peek
105
- rescue StopIteration
106
- break
107
- else
108
- val << sep
131
+ def modulate(modulo)
132
+ if modulo == 1
133
+ to_a
134
+ elsif size % modulo != 0
135
+ raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
136
+ else
137
+ (0...size).each_with_object(Array.new(modulo, [])) do |i, array|
138
+ array[i % modulo] += [self[i]]
109
139
  end
110
140
  end
111
141
  end
112
142
 
113
- block ? enum.each(&block) : enum
114
- end
115
- # rubocop:enable Metrics/MethodLength
143
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
144
+ def occur(amount = nil)
145
+ result = Hash.new { |hash, key| hash[key] = [] }
146
+
147
+ each do |item|
148
+ key = item
149
+ result[key] << item
150
+ end
116
151
 
117
- def many?
118
- found_count = 0
152
+ if block_given?
153
+ result.select! { |_key, values| yield(values.size) }
154
+ else
155
+ raise ArgumentError, 'Invalid occur amount' unless amount
119
156
 
120
- if block_given?
121
- any? do |val|
122
- found_count += 1 if yield(val)
123
- found_count > 1
157
+ if amount.is_a?(Range)
158
+ result.select! { |_key, values| amount.include?(values.size) }
159
+ else
160
+ result.select! { |_key, values| values.size == amount }
161
+ end
124
162
  end
125
- else
126
- any? { (found_count += 1) > 1 }
163
+
164
+ result.values.flatten.uniq
127
165
  end
128
- end
166
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
129
167
 
130
- def modulate(modulo)
131
- if modulo == 1
132
- to_a
133
- elsif size % modulo != 0
134
- raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
135
- else
136
- (0...size).each_with_object(Array.new(modulo, [])) do |i, array|
137
- array[i % modulo] += [self[i]]
168
+ def produce(identity = 0, &block)
169
+ if block_given?
170
+ map(&block).produce(identity)
171
+ else
172
+ inject { |acc, val| acc * val } || identity
138
173
  end
139
174
  end
140
- end
141
175
 
142
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
143
- def occur(amount = nil)
144
- result = Hash.new { |hash, key| hash[key] = [] }
145
-
146
- each do |item|
147
- key = item
148
- result[key] << item
176
+ def quotient(identity = 0, &block)
177
+ if block_given?
178
+ map(&block).quotient(identity)
179
+ else
180
+ inject { |acc, val| acc / val } || identity
181
+ end
149
182
  end
150
183
 
151
- if block_given?
152
- result.select! { |_key, values| yield(values.size) }
153
- else
154
- raise ArgumentError, 'Invalid occur amount' unless amount
184
+ def several?
185
+ found_count = 0
155
186
 
156
- if amount.is_a?(Range)
157
- result.select! { |_key, values| amount.include?(values.size) }
187
+ if block_given?
188
+ each { |*opt| found_count += 1 if yield(*opt) }
158
189
  else
159
- result.select! { |_key, values| values.size == amount }
190
+ each { |opt| found_count += 1 if opt }
160
191
  end
161
- end
162
-
163
- result.values.flatten.uniq
164
- end
165
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
166
192
 
167
- def produce(identity = 0, &block)
168
- if block_given?
169
- map(&block).produce(identity)
170
- else
171
- inject { |acc, val| acc * val } || identity
193
+ found_count > 1
172
194
  end
173
- end
174
195
 
175
- def quotient(identity = 0, &block)
176
- if block_given?
177
- map(&block).quotient(identity)
178
- else
179
- inject { |acc, val| acc / val } || identity
196
+ # rubocop:disable Metrics/MethodLength
197
+ def squeeze(*limited_to)
198
+ first = true
199
+ current = nil
200
+
201
+ each_with_object([]) do |val, array|
202
+ if !limited_to.empty? && !limited_to.include?(val)
203
+ array << val
204
+ elsif first || current != val
205
+ array << val
206
+ first = false
207
+ current = val
208
+ end
209
+ end
180
210
  end
181
- end
211
+ # rubocop:enable Metrics/MethodLength
182
212
 
183
- def several?
184
- found_count = 0
213
+ def take_last(num)
214
+ collection_size = to_a.size
215
+ return self if num > collection_size
185
216
 
186
- if block_given?
187
- each { |*opt| found_count += 1 if yield(*opt) }
188
- else
189
- each { |opt| found_count += 1 if opt }
217
+ self[(collection_size - num)..-1]
190
218
  end
191
219
 
192
- found_count > 1
193
- end
220
+ def take_last_if
221
+ reverse_each.with_object([]) do |val, arr|
222
+ break arr unless yield(val)
194
223
 
195
- # rubocop:disable Metrics/MethodLength
196
- def squeeze(*limited_to)
197
- first = true
198
- current = nil
199
-
200
- each_with_object([]) do |val, array|
201
- if !limited_to.empty? && !limited_to.include?(val)
202
- array << val
203
- elsif first || current != val
204
- array << val
205
- first = false
206
- current = val
224
+ arr.unshift(val)
207
225
  end
208
226
  end
209
- end
210
- # rubocop:enable Metrics/MethodLength
211
-
212
- def take_last(num)
213
- collection_size = to_a.size
214
- return self if num > collection_size
215
-
216
- self[(collection_size - num)..-1]
217
- end
218
-
219
- def take_last_if
220
- reverse_each.with_object([]) do |val, arr|
221
- break arr unless yield(val)
222
227
 
223
- arr.unshift(val)
224
- end
225
228
  end
226
-
227
229
  end