tbpgr_utils 0.0.25 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +23 -0
- data/lib/open_classes/array.rb +411 -382
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array_spec.rb +57 -2
- metadata +12 -12
data/README.md
CHANGED
@@ -28,6 +28,7 @@ Or install it yourself as:
|
|
28
28
|
|[TbpgrUtils Array#together_concat](#arraytogether_concat) |together version of Array#concat. together_concat has alias :tconcat |
|
29
29
|
|[TbpgrUtils Array#together_delete](#arraytogether_delete) |together version of Array#delete. together_delete has alias :tdelete |
|
30
30
|
|[TbpgrUtils Array#together_delete_at](#arraytogether_delete_at) |together version of Array#delete_at. together_delete_at has alias :tdelete_at |
|
31
|
+
|[TbpgrUtils Array#together_delete_if](#arraytogether_delete_if) |together version of Array#delete_if. together_delete_if has alias :tdelete_if |
|
31
32
|
|[TbpgrUtils Array#together_map](#arraytogether_mapor-tmap-together_collect-tcollect) |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
|
32
33
|
|[TbpgrUtils Array#together_map!](#arraytogether_map-1or-tmap-1-together_collect-1-tcollect-1) |together version of Enumerable#map!. together_map! has aliases [:tmap!, :together_collect!, :tcollect!] |
|
33
34
|
|[TbpgrUtils Array#together_reduce](#arraytogether_reduceor-treduce-together_inject-tinject) |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
|
@@ -212,6 +213,27 @@ print lists # => output [[1, 3, 4], [2, 4, 5]]
|
|
212
213
|
|
213
214
|
[back to list](#list)
|
214
215
|
|
216
|
+
### Array#together_delete_if
|
217
|
+
if delete_if target is exist
|
218
|
+
~~~ruby
|
219
|
+
require 'tbpgr_utils'
|
220
|
+
|
221
|
+
lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
|
222
|
+
ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
223
|
+
print ret # => [[2, 4], [4, 8]]
|
224
|
+
~~~
|
225
|
+
|
226
|
+
if delete_if target is not exist. return nil.
|
227
|
+
~~~ruby
|
228
|
+
require 'tbpgr_utils'
|
229
|
+
|
230
|
+
lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
|
231
|
+
ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
232
|
+
print ret # => nil
|
233
|
+
~~~
|
234
|
+
|
235
|
+
[back to list](#list)
|
236
|
+
|
215
237
|
### Array#together_map(or tmap, together_collect, tcollect)
|
216
238
|
~~~ruby
|
217
239
|
require 'tbpgr_utils'
|
@@ -936,6 +958,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
936
958
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
937
959
|
|
938
960
|
## History
|
961
|
+
* version 0.0.26 : add Array#together_delete_if(alias tdelete_if)
|
939
962
|
* version 0.0.25 : add Array#together_delete_at(alias tdelete_at)
|
940
963
|
* version 0.0.24 : add Array#together_delete(alias tdelete)
|
941
964
|
* version 0.0.23 : add Array#together_map!(aliases => [tmap!, together_collect!, tcollect!])
|
data/lib/open_classes/array.rb
CHANGED
@@ -1,382 +1,411 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'open_classes/object'
|
3
|
-
require 'open_classes/module'
|
4
|
-
|
5
|
-
# Array
|
6
|
-
class Array
|
7
|
-
# Arrays loop together.
|
8
|
-
#
|
9
|
-
# alpha = %w{one two three}
|
10
|
-
# numbers = %w{1 2 3}
|
11
|
-
# [alpha, numbers].together do |first, second|
|
12
|
-
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
13
|
-
# end
|
14
|
-
def together
|
15
|
-
if_not_contain_array_rails_type_error
|
16
|
-
first.each_with_index do |i_v, i|
|
17
|
-
eval_each_str = get_args_str_for_together i
|
18
|
-
instance_eval "yield(#{eval_each_str})"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
# Arrays loop together with index.
|
23
|
-
#
|
24
|
-
# alpha = %w{one two three}
|
25
|
-
# numbers = %w{1 2 3}
|
26
|
-
# [alpha, numbers].together_with_index do |first, second, index|
|
27
|
-
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
28
|
-
# end
|
29
|
-
def together_with_index
|
30
|
-
if_not_contain_array_rails_type_error
|
31
|
-
first.each_with_index do |i_v, i|
|
32
|
-
eval_each_str = get_args_str_for_together i, true
|
33
|
-
instance_eval "yield(#{eval_each_str})"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Arrays together map.
|
38
|
-
#
|
39
|
-
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
40
|
-
#
|
41
|
-
# if you want to single Array return
|
42
|
-
# alpha = %w{one two three}
|
43
|
-
# numbers = %w{1 2 3}
|
44
|
-
# print [alpha, numbers].together_map do |first, second|
|
45
|
-
# "#{first}:#{second}\n"
|
46
|
-
# end # => output one:1, two:2, three:3
|
47
|
-
#
|
48
|
-
# if you want to multi Array return
|
49
|
-
# alpha = %w{one two three}
|
50
|
-
# numbers = %w{1 2 3}
|
51
|
-
# print [alpha, numbers].together_map do |first, second|
|
52
|
-
# ["#{first}:#{second}", "#{second}:#{first}"]
|
53
|
-
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
54
|
-
def together_map
|
55
|
-
if_not_contain_array_rails_type_error
|
56
|
-
ret = []
|
57
|
-
first.each_with_index do |i_v, i|
|
58
|
-
eval_each_str = get_args_str_for_together i
|
59
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
60
|
-
ret = set_together_each_return_map(ret, each_ret, i)
|
61
|
-
end
|
62
|
-
ret
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
# Arrays together map!.
|
67
|
-
#
|
68
|
-
# together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
|
69
|
-
#
|
70
|
-
# if you want to single Array return
|
71
|
-
# alpha = %w{one two three}
|
72
|
-
# numbers = %w{1 2 3}
|
73
|
-
# ary = [alpha, numbers]
|
74
|
-
# ret = ary.together_map! do |first, second|
|
75
|
-
# "#{first}:#{second}"
|
76
|
-
# end
|
77
|
-
# print ret # => output ['one:1', 'two:2', 'three:3']
|
78
|
-
# print ary # => output ['one:1', 'two:2', 'three:3']
|
79
|
-
#
|
80
|
-
# if you want to multi Array return
|
81
|
-
# alpha = %w{one two three}
|
82
|
-
# numbers = %w{1 2 3}
|
83
|
-
# ary = [alpha, numbers]
|
84
|
-
# ret = ary.together_map! do |first, second|
|
85
|
-
# ["#{first}:#{second}", "#{second}:#{first}"]
|
86
|
-
# end
|
87
|
-
# print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
88
|
-
# print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
89
|
-
def together_map!
|
90
|
-
if_not_contain_array_rails_type_error
|
91
|
-
ret = []
|
92
|
-
first.each_with_index do |i_v, i|
|
93
|
-
eval_each_str = get_args_str_for_together i
|
94
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
95
|
-
ret = set_together_each_return_map(ret, each_ret, i)
|
96
|
-
end
|
97
|
-
clear
|
98
|
-
ret.each { |v|self << v }
|
99
|
-
end
|
100
|
-
|
101
|
-
# Arrays loop together select.
|
102
|
-
#
|
103
|
-
# together_select has aliases [:tselect, :together_find_all, :tfindall]
|
104
|
-
#
|
105
|
-
# if you want to single Array return
|
106
|
-
# firsts = [1, 2, 3, 4]
|
107
|
-
# seconds = [4, 2, 3, 1]
|
108
|
-
# ret = [firsts, seconds].together_select{|first, second|first == second}
|
109
|
-
# print ret # => output [[2, 3], [2, 3]]
|
110
|
-
#
|
111
|
-
# if you want to multi Array return
|
112
|
-
# firsts = [1, 2, 3, 4]
|
113
|
-
# seconds = [4, 2, 3, 1]
|
114
|
-
# ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
|
115
|
-
# print ret # => output [[1, 3], [4, 2]]
|
116
|
-
def together_select
|
117
|
-
if_not_contain_array_rails_type_error
|
118
|
-
ret = []
|
119
|
-
first.each_with_index do |i_v, i|
|
120
|
-
eval_each_str = get_args_str_for_together i
|
121
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
122
|
-
ret = set_together_each_return_select(ret, each_ret, i)
|
123
|
-
end
|
124
|
-
ret
|
125
|
-
end
|
126
|
-
|
127
|
-
# Arrays loop together reduce.
|
128
|
-
#
|
129
|
-
# together_reduce has aliases [:treduce, :together_inject, :tinject]
|
130
|
-
#
|
131
|
-
# if you want to single return
|
132
|
-
# firsts = [1, 2, 3, 4]
|
133
|
-
# seconds = [4, 2, 3, 1]
|
134
|
-
# ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
|
135
|
-
# print ret # => output 20
|
136
|
-
#
|
137
|
-
# if you want to single return with init value
|
138
|
-
# firsts = [1, 2, 3, 4]
|
139
|
-
# seconds = [4, 2, 3, 1]
|
140
|
-
# ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
|
141
|
-
# print ret # => output 30
|
142
|
-
#
|
143
|
-
# if you want to single return with init string value
|
144
|
-
# firsts = %w{a b c}
|
145
|
-
# seconds = %w{1 2 3}
|
146
|
-
# ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
|
147
|
-
# print ret # => output 'start-a1b2c3'
|
148
|
-
#
|
149
|
-
# if you want to single return with init Array value
|
150
|
-
# firsts = [1, 2, 3, 4]
|
151
|
-
# seconds = [4, 2, 3, 1]
|
152
|
-
# ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
|
153
|
-
# print ret # => output [5, 4, 6, 5]
|
154
|
-
#
|
155
|
-
# if you want to single return with init Hash value
|
156
|
-
# firsts = [1, 2, 3, 4]
|
157
|
-
# seconds = [4, 2, 3, 1]
|
158
|
-
# ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
|
159
|
-
# print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
|
160
|
-
def together_reduce(init = nil)
|
161
|
-
if_not_contain_array_rails_type_error
|
162
|
-
memo = initial_memo init
|
163
|
-
first.each_with_index do |i_v, i|
|
164
|
-
eval_each_str = get_args_str_for_together i
|
165
|
-
memo = instance_eval "yield(memo, #{eval_each_str})"
|
166
|
-
end
|
167
|
-
memo
|
168
|
-
end
|
169
|
-
|
170
|
-
# Arrays bulk concat.
|
171
|
-
#
|
172
|
-
# together_concat has alias :tconcat
|
173
|
-
#
|
174
|
-
# alpha = %w{one two three}
|
175
|
-
# numbers = %w{1 2 3}
|
176
|
-
# [alpha, numbers].together do |first, second|
|
177
|
-
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
178
|
-
# end
|
179
|
-
def together_concat(other)
|
180
|
-
if_not_contain_array_rails_type_error
|
181
|
-
each { |list|list.concat other }
|
182
|
-
end
|
183
|
-
|
184
|
-
# Arrays bulk at.
|
185
|
-
#
|
186
|
-
# together_at has alias :tat
|
187
|
-
#
|
188
|
-
# same elements size case
|
189
|
-
# alpha = %w{one two three}
|
190
|
-
# numbers = %w{1 2 3}
|
191
|
-
# [alpha, numbers].together_at 2 # => output ['three', 3]
|
192
|
-
#
|
193
|
-
# different elements size case
|
194
|
-
# alpha = %w{one two three}
|
195
|
-
# numbers = %w{1 2}
|
196
|
-
# [alpha, numbers].together_at 2 # => output ['three', nil]
|
197
|
-
def together_at(index)
|
198
|
-
if_not_contain_array_rails_type_error
|
199
|
-
reduce([]) { |ats, list|ats << list.at(index) }
|
200
|
-
end
|
201
|
-
|
202
|
-
# Arrays bulk clear.
|
203
|
-
#
|
204
|
-
# together_clear has alias :tclear
|
205
|
-
#
|
206
|
-
# same elements size case
|
207
|
-
# alpha = %w{one two three}
|
208
|
-
# numbers = %w{1 2 3}
|
209
|
-
# [alpha, numbers].together_clear # => output [[],[]]
|
210
|
-
def together_clear
|
211
|
-
if_not_contain_array_rails_type_error
|
212
|
-
each { |list|list.clear }
|
213
|
-
end
|
214
|
-
|
215
|
-
# Arrays bulk compact.(immutable)
|
216
|
-
#
|
217
|
-
# together_compact has alias :tcompact
|
218
|
-
#
|
219
|
-
# same elements size case
|
220
|
-
# alpha = ['a','b','c', nil,'d']
|
221
|
-
# numbers = [1, 2, nil, 3]
|
222
|
-
# lists = [alpha, numbers]
|
223
|
-
# ret = lists.together_compact
|
224
|
-
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
225
|
-
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
226
|
-
def together_compact
|
227
|
-
if_not_contain_array_rails_type_error
|
228
|
-
reduce([]) { |ret, list|ret << list.compact }
|
229
|
-
end
|
230
|
-
|
231
|
-
# Arrays bulk compact!.(mutable)
|
232
|
-
#
|
233
|
-
# together_compact! has alias :tcompact!
|
234
|
-
#
|
235
|
-
# same elements size case
|
236
|
-
# alpha = ['a','b','c', nil,'d']
|
237
|
-
# numbers = [1, 2, nil, 3]
|
238
|
-
# lists = [alpha, numbers]
|
239
|
-
# ret = lists.together_compact!
|
240
|
-
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
241
|
-
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
242
|
-
def together_compact!
|
243
|
-
if_not_contain_array_rails_type_error
|
244
|
-
each { |list|list.compact! }
|
245
|
-
end
|
246
|
-
|
247
|
-
# Arrays bulk delete.
|
248
|
-
#
|
249
|
-
# together_delete has alias :tdelete
|
250
|
-
#
|
251
|
-
# if delete target is exist
|
252
|
-
# child1 = [1, 2, 3, 4]
|
253
|
-
# child2 = [2, 3, 4, 5]
|
254
|
-
# lists = [child1, child2]
|
255
|
-
# ret = lists.together_delete 2
|
256
|
-
# print ret # => 2
|
257
|
-
# print lists # => output [[1, 3, 4], [3, 4, 5]]
|
258
|
-
#
|
259
|
-
# if delete target is not exist
|
260
|
-
# child1 = [1, 2, 3, 4]
|
261
|
-
# child2 = [2, 3, 4, 5]
|
262
|
-
# lists = [child1, child2]
|
263
|
-
# ret = lists.together_delete 6
|
264
|
-
# print ret # => nil
|
265
|
-
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
266
|
-
#
|
267
|
-
# if delete target is not exist and use block
|
268
|
-
# child1 = [1, 2, 3, 4]
|
269
|
-
# child2 = [2, 3, 4, 5]
|
270
|
-
# lists = [child1, child2]
|
271
|
-
# ret = lists.together_delete(6) { 999 }
|
272
|
-
# print ret # => 999
|
273
|
-
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
274
|
-
def together_delete(value)
|
275
|
-
if_not_contain_array_rails_type_error
|
276
|
-
ret = []
|
277
|
-
each { |list|ret << list.delete(value) }
|
278
|
-
default_return = block_given? ? yield : nil
|
279
|
-
ret.compact.size == 0 ? default_return : value
|
280
|
-
end
|
281
|
-
|
282
|
-
# Arrays bulk delete_at.
|
283
|
-
#
|
284
|
-
# together_delete_at has alias :tdelete_at
|
285
|
-
#
|
286
|
-
# if delete_at target is exist
|
287
|
-
# child1 = [1, 2, 3, 4]
|
288
|
-
# child2 = [2, 3, 4, 5]
|
289
|
-
# lists = [child1, child2]
|
290
|
-
# ret = lists.together_delete_at 2
|
291
|
-
# print ret # => [3, 4]
|
292
|
-
# print lists # => output [[1, 2, 4], [2, 3, 5]]
|
293
|
-
#
|
294
|
-
# if delete_at target is not exist
|
295
|
-
# child1 = [1, 2, 3, 4]
|
296
|
-
# child2 = [2, 3, 4, 5]
|
297
|
-
# lists = [child1, child2]
|
298
|
-
# ret = lists.together_delete_at 6
|
299
|
-
# print ret # => [nil, nil]
|
300
|
-
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
301
|
-
#
|
302
|
-
# if delete_at target is exist(minus index)
|
303
|
-
# child1 = [1, 2, 3, 4]
|
304
|
-
# child2 = [2, 3, 4, 5]
|
305
|
-
# lists = [child1, child2]
|
306
|
-
# ret = lists.together_delete_at -3
|
307
|
-
# print ret # => [2, 3]
|
308
|
-
# print lists # => output [[1, 3, 4], [2, 4, 5]]
|
309
|
-
def together_delete_at(index)
|
310
|
-
if_not_contain_array_rails_type_error
|
311
|
-
reduce([]) { |ret, list|ret << list.delete_at(index) }
|
312
|
-
end
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
require 'open_classes/module'
|
4
|
+
|
5
|
+
# Array
|
6
|
+
class Array
|
7
|
+
# Arrays loop together.
|
8
|
+
#
|
9
|
+
# alpha = %w{one two three}
|
10
|
+
# numbers = %w{1 2 3}
|
11
|
+
# [alpha, numbers].together do |first, second|
|
12
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
13
|
+
# end
|
14
|
+
def together
|
15
|
+
if_not_contain_array_rails_type_error
|
16
|
+
first.each_with_index do |i_v, i|
|
17
|
+
eval_each_str = get_args_str_for_together i
|
18
|
+
instance_eval "yield(#{eval_each_str})"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Arrays loop together with index.
|
23
|
+
#
|
24
|
+
# alpha = %w{one two three}
|
25
|
+
# numbers = %w{1 2 3}
|
26
|
+
# [alpha, numbers].together_with_index do |first, second, index|
|
27
|
+
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
28
|
+
# end
|
29
|
+
def together_with_index
|
30
|
+
if_not_contain_array_rails_type_error
|
31
|
+
first.each_with_index do |i_v, i|
|
32
|
+
eval_each_str = get_args_str_for_together i, true
|
33
|
+
instance_eval "yield(#{eval_each_str})"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Arrays together map.
|
38
|
+
#
|
39
|
+
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
40
|
+
#
|
41
|
+
# if you want to single Array return
|
42
|
+
# alpha = %w{one two three}
|
43
|
+
# numbers = %w{1 2 3}
|
44
|
+
# print [alpha, numbers].together_map do |first, second|
|
45
|
+
# "#{first}:#{second}\n"
|
46
|
+
# end # => output one:1, two:2, three:3
|
47
|
+
#
|
48
|
+
# if you want to multi Array return
|
49
|
+
# alpha = %w{one two three}
|
50
|
+
# numbers = %w{1 2 3}
|
51
|
+
# print [alpha, numbers].together_map do |first, second|
|
52
|
+
# ["#{first}:#{second}", "#{second}:#{first}"]
|
53
|
+
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
54
|
+
def together_map
|
55
|
+
if_not_contain_array_rails_type_error
|
56
|
+
ret = []
|
57
|
+
first.each_with_index do |i_v, i|
|
58
|
+
eval_each_str = get_args_str_for_together i
|
59
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
60
|
+
ret = set_together_each_return_map(ret, each_ret, i)
|
61
|
+
end
|
62
|
+
ret
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
# Arrays together map!.
|
67
|
+
#
|
68
|
+
# together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
|
69
|
+
#
|
70
|
+
# if you want to single Array return
|
71
|
+
# alpha = %w{one two three}
|
72
|
+
# numbers = %w{1 2 3}
|
73
|
+
# ary = [alpha, numbers]
|
74
|
+
# ret = ary.together_map! do |first, second|
|
75
|
+
# "#{first}:#{second}"
|
76
|
+
# end
|
77
|
+
# print ret # => output ['one:1', 'two:2', 'three:3']
|
78
|
+
# print ary # => output ['one:1', 'two:2', 'three:3']
|
79
|
+
#
|
80
|
+
# if you want to multi Array return
|
81
|
+
# alpha = %w{one two three}
|
82
|
+
# numbers = %w{1 2 3}
|
83
|
+
# ary = [alpha, numbers]
|
84
|
+
# ret = ary.together_map! do |first, second|
|
85
|
+
# ["#{first}:#{second}", "#{second}:#{first}"]
|
86
|
+
# end
|
87
|
+
# print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
88
|
+
# print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
89
|
+
def together_map!
|
90
|
+
if_not_contain_array_rails_type_error
|
91
|
+
ret = []
|
92
|
+
first.each_with_index do |i_v, i|
|
93
|
+
eval_each_str = get_args_str_for_together i
|
94
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
95
|
+
ret = set_together_each_return_map(ret, each_ret, i)
|
96
|
+
end
|
97
|
+
clear
|
98
|
+
ret.each { |v|self << v }
|
99
|
+
end
|
100
|
+
|
101
|
+
# Arrays loop together select.
|
102
|
+
#
|
103
|
+
# together_select has aliases [:tselect, :together_find_all, :tfindall]
|
104
|
+
#
|
105
|
+
# if you want to single Array return
|
106
|
+
# firsts = [1, 2, 3, 4]
|
107
|
+
# seconds = [4, 2, 3, 1]
|
108
|
+
# ret = [firsts, seconds].together_select{|first, second|first == second}
|
109
|
+
# print ret # => output [[2, 3], [2, 3]]
|
110
|
+
#
|
111
|
+
# if you want to multi Array return
|
112
|
+
# firsts = [1, 2, 3, 4]
|
113
|
+
# seconds = [4, 2, 3, 1]
|
114
|
+
# ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
|
115
|
+
# print ret # => output [[1, 3], [4, 2]]
|
116
|
+
def together_select
|
117
|
+
if_not_contain_array_rails_type_error
|
118
|
+
ret = []
|
119
|
+
first.each_with_index do |i_v, i|
|
120
|
+
eval_each_str = get_args_str_for_together i
|
121
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
122
|
+
ret = set_together_each_return_select(ret, each_ret, i)
|
123
|
+
end
|
124
|
+
ret
|
125
|
+
end
|
126
|
+
|
127
|
+
# Arrays loop together reduce.
|
128
|
+
#
|
129
|
+
# together_reduce has aliases [:treduce, :together_inject, :tinject]
|
130
|
+
#
|
131
|
+
# if you want to single return
|
132
|
+
# firsts = [1, 2, 3, 4]
|
133
|
+
# seconds = [4, 2, 3, 1]
|
134
|
+
# ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
|
135
|
+
# print ret # => output 20
|
136
|
+
#
|
137
|
+
# if you want to single return with init value
|
138
|
+
# firsts = [1, 2, 3, 4]
|
139
|
+
# seconds = [4, 2, 3, 1]
|
140
|
+
# ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
|
141
|
+
# print ret # => output 30
|
142
|
+
#
|
143
|
+
# if you want to single return with init string value
|
144
|
+
# firsts = %w{a b c}
|
145
|
+
# seconds = %w{1 2 3}
|
146
|
+
# ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
|
147
|
+
# print ret # => output 'start-a1b2c3'
|
148
|
+
#
|
149
|
+
# if you want to single return with init Array value
|
150
|
+
# firsts = [1, 2, 3, 4]
|
151
|
+
# seconds = [4, 2, 3, 1]
|
152
|
+
# ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
|
153
|
+
# print ret # => output [5, 4, 6, 5]
|
154
|
+
#
|
155
|
+
# if you want to single return with init Hash value
|
156
|
+
# firsts = [1, 2, 3, 4]
|
157
|
+
# seconds = [4, 2, 3, 1]
|
158
|
+
# ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
|
159
|
+
# print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
|
160
|
+
def together_reduce(init = nil)
|
161
|
+
if_not_contain_array_rails_type_error
|
162
|
+
memo = initial_memo init
|
163
|
+
first.each_with_index do |i_v, i|
|
164
|
+
eval_each_str = get_args_str_for_together i
|
165
|
+
memo = instance_eval "yield(memo, #{eval_each_str})"
|
166
|
+
end
|
167
|
+
memo
|
168
|
+
end
|
169
|
+
|
170
|
+
# Arrays bulk concat.
|
171
|
+
#
|
172
|
+
# together_concat has alias :tconcat
|
173
|
+
#
|
174
|
+
# alpha = %w{one two three}
|
175
|
+
# numbers = %w{1 2 3}
|
176
|
+
# [alpha, numbers].together do |first, second|
|
177
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
178
|
+
# end
|
179
|
+
def together_concat(other)
|
180
|
+
if_not_contain_array_rails_type_error
|
181
|
+
each { |list|list.concat other }
|
182
|
+
end
|
183
|
+
|
184
|
+
# Arrays bulk at.
|
185
|
+
#
|
186
|
+
# together_at has alias :tat
|
187
|
+
#
|
188
|
+
# same elements size case
|
189
|
+
# alpha = %w{one two three}
|
190
|
+
# numbers = %w{1 2 3}
|
191
|
+
# [alpha, numbers].together_at 2 # => output ['three', 3]
|
192
|
+
#
|
193
|
+
# different elements size case
|
194
|
+
# alpha = %w{one two three}
|
195
|
+
# numbers = %w{1 2}
|
196
|
+
# [alpha, numbers].together_at 2 # => output ['three', nil]
|
197
|
+
def together_at(index)
|
198
|
+
if_not_contain_array_rails_type_error
|
199
|
+
reduce([]) { |ats, list|ats << list.at(index) }
|
200
|
+
end
|
201
|
+
|
202
|
+
# Arrays bulk clear.
|
203
|
+
#
|
204
|
+
# together_clear has alias :tclear
|
205
|
+
#
|
206
|
+
# same elements size case
|
207
|
+
# alpha = %w{one two three}
|
208
|
+
# numbers = %w{1 2 3}
|
209
|
+
# [alpha, numbers].together_clear # => output [[],[]]
|
210
|
+
def together_clear
|
211
|
+
if_not_contain_array_rails_type_error
|
212
|
+
each { |list|list.clear }
|
213
|
+
end
|
214
|
+
|
215
|
+
# Arrays bulk compact.(immutable)
|
216
|
+
#
|
217
|
+
# together_compact has alias :tcompact
|
218
|
+
#
|
219
|
+
# same elements size case
|
220
|
+
# alpha = ['a','b','c', nil,'d']
|
221
|
+
# numbers = [1, 2, nil, 3]
|
222
|
+
# lists = [alpha, numbers]
|
223
|
+
# ret = lists.together_compact
|
224
|
+
# print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
|
225
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
226
|
+
def together_compact
|
227
|
+
if_not_contain_array_rails_type_error
|
228
|
+
reduce([]) { |ret, list|ret << list.compact }
|
229
|
+
end
|
230
|
+
|
231
|
+
# Arrays bulk compact!.(mutable)
|
232
|
+
#
|
233
|
+
# together_compact! has alias :tcompact!
|
234
|
+
#
|
235
|
+
# same elements size case
|
236
|
+
# alpha = ['a','b','c', nil,'d']
|
237
|
+
# numbers = [1, 2, nil, 3]
|
238
|
+
# lists = [alpha, numbers]
|
239
|
+
# ret = lists.together_compact!
|
240
|
+
# print lists # => output [['a','b','c','d'], [1, 2, 3]]
|
241
|
+
# print ret # => output [['a','b','c','d'], [1, 2, 3]]
|
242
|
+
def together_compact!
|
243
|
+
if_not_contain_array_rails_type_error
|
244
|
+
each { |list|list.compact! }
|
245
|
+
end
|
246
|
+
|
247
|
+
# Arrays bulk delete.
|
248
|
+
#
|
249
|
+
# together_delete has alias :tdelete
|
250
|
+
#
|
251
|
+
# if delete target is exist
|
252
|
+
# child1 = [1, 2, 3, 4]
|
253
|
+
# child2 = [2, 3, 4, 5]
|
254
|
+
# lists = [child1, child2]
|
255
|
+
# ret = lists.together_delete 2
|
256
|
+
# print ret # => 2
|
257
|
+
# print lists # => output [[1, 3, 4], [3, 4, 5]]
|
258
|
+
#
|
259
|
+
# if delete target is not exist
|
260
|
+
# child1 = [1, 2, 3, 4]
|
261
|
+
# child2 = [2, 3, 4, 5]
|
262
|
+
# lists = [child1, child2]
|
263
|
+
# ret = lists.together_delete 6
|
264
|
+
# print ret # => nil
|
265
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
266
|
+
#
|
267
|
+
# if delete target is not exist and use block
|
268
|
+
# child1 = [1, 2, 3, 4]
|
269
|
+
# child2 = [2, 3, 4, 5]
|
270
|
+
# lists = [child1, child2]
|
271
|
+
# ret = lists.together_delete(6) { 999 }
|
272
|
+
# print ret # => 999
|
273
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
274
|
+
def together_delete(value)
|
275
|
+
if_not_contain_array_rails_type_error
|
276
|
+
ret = []
|
277
|
+
each { |list|ret << list.delete(value) }
|
278
|
+
default_return = block_given? ? yield : nil
|
279
|
+
ret.compact.size == 0 ? default_return : value
|
280
|
+
end
|
281
|
+
|
282
|
+
# Arrays bulk delete_at.
|
283
|
+
#
|
284
|
+
# together_delete_at has alias :tdelete_at
|
285
|
+
#
|
286
|
+
# if delete_at target is exist
|
287
|
+
# child1 = [1, 2, 3, 4]
|
288
|
+
# child2 = [2, 3, 4, 5]
|
289
|
+
# lists = [child1, child2]
|
290
|
+
# ret = lists.together_delete_at 2
|
291
|
+
# print ret # => [3, 4]
|
292
|
+
# print lists # => output [[1, 2, 4], [2, 3, 5]]
|
293
|
+
#
|
294
|
+
# if delete_at target is not exist
|
295
|
+
# child1 = [1, 2, 3, 4]
|
296
|
+
# child2 = [2, 3, 4, 5]
|
297
|
+
# lists = [child1, child2]
|
298
|
+
# ret = lists.together_delete_at 6
|
299
|
+
# print ret # => [nil, nil]
|
300
|
+
# print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]
|
301
|
+
#
|
302
|
+
# if delete_at target is exist(minus index)
|
303
|
+
# child1 = [1, 2, 3, 4]
|
304
|
+
# child2 = [2, 3, 4, 5]
|
305
|
+
# lists = [child1, child2]
|
306
|
+
# ret = lists.together_delete_at -3
|
307
|
+
# print ret # => [2, 3]
|
308
|
+
# print lists # => output [[1, 3, 4], [2, 4, 5]]
|
309
|
+
def together_delete_at(index)
|
310
|
+
if_not_contain_array_rails_type_error
|
311
|
+
reduce([]) { |ret, list|ret << list.delete_at(index) }
|
312
|
+
end
|
313
|
+
|
314
|
+
|
315
|
+
# Arrays bulk delete_if.
|
316
|
+
#
|
317
|
+
# together_delete_if has alias :tdelete_if
|
318
|
+
#
|
319
|
+
# if delete_if target is exist. return self.
|
320
|
+
# lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
|
321
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
322
|
+
# print ret # => [[2, 4], [4, 8]]
|
323
|
+
#
|
324
|
+
# if delete_if target is not exist. return nil.
|
325
|
+
# lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
|
326
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
327
|
+
# print ret # => nil
|
328
|
+
def together_delete_if(&block)
|
329
|
+
if_not_contain_array_rails_type_error
|
330
|
+
have_deleted = false
|
331
|
+
first.each_with_index do |i_v, i|
|
332
|
+
eval_each_str = get_args_str_for_together i
|
333
|
+
is_delete = instance_eval "yield(#{eval_each_str})"
|
334
|
+
if is_delete
|
335
|
+
each { |e|e.delete_at i }
|
336
|
+
have_deleted = true
|
337
|
+
end
|
338
|
+
end
|
339
|
+
have_deleted ? self : nil
|
340
|
+
end
|
341
|
+
|
342
|
+
private
|
343
|
+
|
344
|
+
def if_not_contain_array_rails_type_error
|
345
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
346
|
+
end
|
347
|
+
|
348
|
+
def get_args_for_together(i)
|
349
|
+
eval_each = []
|
350
|
+
each_with_index { |j_v, j|eval_each << "self[#{j}][#{i}]" }
|
351
|
+
eval_each
|
352
|
+
end
|
353
|
+
|
354
|
+
def get_args_str_for_together(i, with_index = false)
|
355
|
+
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
356
|
+
each_eval.join(',')
|
357
|
+
end
|
358
|
+
|
359
|
+
def set_together_each_return_map(ret, each_ret, index)
|
360
|
+
if together_return_multi?(each_ret)
|
361
|
+
size.times { |i|ret << [] } if index == 0
|
362
|
+
(0..(size - 1)).each { |i|ret[i] << each_ret[i] }
|
363
|
+
else
|
364
|
+
ret << each_ret
|
365
|
+
end
|
366
|
+
ret
|
367
|
+
end
|
368
|
+
|
369
|
+
# def set_together_each_return_map!(ret, each_ret, index)
|
370
|
+
# if together_return_multi?(each_ret)
|
371
|
+
# size.times { |i|ret << [] } if index == 0
|
372
|
+
# (0..(size - 1)).each { |i|ret[i] each_ret[i] }
|
373
|
+
# else
|
374
|
+
# ret << each_ret
|
375
|
+
# end
|
376
|
+
# ret
|
377
|
+
# end
|
378
|
+
|
379
|
+
def set_together_each_return_select(ret, each_ret, index)
|
380
|
+
unless together_return_multi?(each_ret)
|
381
|
+
tmp_each_ret = []
|
382
|
+
size.times { tmp_each_ret << each_ret }
|
383
|
+
each_ret = tmp_each_ret
|
384
|
+
end
|
385
|
+
size.times { |i|ret << [] } if index == 0
|
386
|
+
(0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] }
|
387
|
+
ret
|
388
|
+
end
|
389
|
+
|
390
|
+
def initial_memo(init)
|
391
|
+
return init unless init.nil?
|
392
|
+
first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
|
393
|
+
end
|
394
|
+
|
395
|
+
def together_return_multi?(list)
|
396
|
+
(list.class == Array && list.size == size).to_bool
|
397
|
+
end
|
398
|
+
|
399
|
+
alias_method :tconcat, :together_concat
|
400
|
+
alias_method :tat, :together_at
|
401
|
+
alias_method :tclear, :together_clear
|
402
|
+
alias_method :tcompact, :together_compact
|
403
|
+
alias_method :tcompact!, :together_compact!
|
404
|
+
alias_method :tdelete, :together_delete
|
405
|
+
alias_method :tdelete_at, :together_delete_at
|
406
|
+
alias_method :tdelete_if, :together_delete_if
|
407
|
+
alias_methods [:together_collect, :tmap, :tcollect], :together_map
|
408
|
+
alias_methods [:together_collect!, :tmap!, :tcollect!], :together_map!
|
409
|
+
alias_methods [:together_find_all, :tselect, :tfindall], :together_select
|
410
|
+
alias_methods [:together_inject, :treduce, :tinject], :together_reduce
|
411
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -750,7 +750,6 @@ describe Array do
|
|
750
750
|
end
|
751
751
|
end
|
752
752
|
|
753
|
-
|
754
753
|
context :together_delete_at do
|
755
754
|
cases = [
|
756
755
|
{
|
@@ -781,7 +780,7 @@ describe Array do
|
|
781
780
|
ret: [2, 3],
|
782
781
|
},
|
783
782
|
{
|
784
|
-
case_no:
|
783
|
+
case_no: 4,
|
785
784
|
case_title: 'valid case(use tdelete_at alias)',
|
786
785
|
inputs: [[1, 2, 3, 4], [2, 3, 4, 5]],
|
787
786
|
delete_value: 2,
|
@@ -823,4 +822,60 @@ describe Array do
|
|
823
822
|
end
|
824
823
|
end
|
825
824
|
end
|
825
|
+
|
826
|
+
context :together_delete_if do
|
827
|
+
cases = [
|
828
|
+
{
|
829
|
+
case_no: 1,
|
830
|
+
case_title: 'valid case',
|
831
|
+
inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
|
832
|
+
delete_block_code: '(first + second).odd?',
|
833
|
+
method_name: :together_delete_if,
|
834
|
+
expected: [[2, 4], [4, 8]],
|
835
|
+
},
|
836
|
+
{
|
837
|
+
case_no: 2,
|
838
|
+
case_title: 'not have delete target value case',
|
839
|
+
inputs: [[2, 2, 4, 4], [6, 4, 6, 8]],
|
840
|
+
delete_block_code: '(first + second).odd?',
|
841
|
+
method_name: :together_delete_if,
|
842
|
+
expected: nil,
|
843
|
+
},
|
844
|
+
{
|
845
|
+
case_no: 3,
|
846
|
+
case_title: 'valid case(alias tdelete_if)',
|
847
|
+
inputs: [[1, 2, 3, 4], [6, 4, 6, 8]],
|
848
|
+
delete_block_code: '(first + second).odd?',
|
849
|
+
method_name: :tdelete_if,
|
850
|
+
expected: [[2, 4], [4, 8]],
|
851
|
+
},
|
852
|
+
]
|
853
|
+
|
854
|
+
cases.each do |c|
|
855
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
856
|
+
begin
|
857
|
+
case_before c
|
858
|
+
|
859
|
+
# -- given --
|
860
|
+
# nothing
|
861
|
+
|
862
|
+
# -- when --
|
863
|
+
actual = c[:inputs].send c[:method_name] {|first, second|eval c[:delete_block_code], binding }
|
864
|
+
|
865
|
+
# -- then --
|
866
|
+
expect(actual).to eq(c[:expected])
|
867
|
+
ensure
|
868
|
+
case_after c
|
869
|
+
end
|
870
|
+
end
|
871
|
+
|
872
|
+
def case_before(c)
|
873
|
+
# implement each case before
|
874
|
+
end
|
875
|
+
|
876
|
+
def case_after(c)
|
877
|
+
# implement each case after
|
878
|
+
end
|
879
|
+
end
|
880
|
+
end
|
826
881
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tbpgr_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.26
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &29275956 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 4.0.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *29275956
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &29275512 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.3'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *29275512
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &29275164 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *29275164
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &21851436 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 2.14.1
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *21851436
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: simplecov
|
60
|
-
requirement: &
|
60
|
+
requirement: &21851136 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 0.8.2
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *21851136
|
69
69
|
description: Utilities
|
70
70
|
email:
|
71
71
|
- tbpgr@tbpgr.jp
|