tbpgr_utils 0.0.33 → 0.0.34
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 +35 -0
- data/lib/open_classes/array.rb +640 -610
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array_spec.rb +1486 -1407
- metadata +12 -12
data/README.md
CHANGED
@@ -37,6 +37,7 @@ Or install it yourself as:
|
|
37
37
|
|[TbpgrUtils Array#together_include?](#arraytogether_include) |together version of Array#include?. together_include? has alias :tinclude? |
|
38
38
|
|[TbpgrUtils Array#together_index](#arraytogether_index) |together version of Array#index. together_index has alias :tindex |
|
39
39
|
|[TbpgrUtils Array#together_insert](#arraytogether_insert) |together version of Array#insert. together_insert has alias :tinsert |
|
40
|
+
|[TbpgrUtils Array#together_last](#arraytogether_last) |together version of Array#last. together_last has alias :tlast |
|
40
41
|
|[TbpgrUtils Array#together_map](#arraytogether_mapor-tmap-together_collect-tcollect) |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
|
41
42
|
|[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!] |
|
42
43
|
|[TbpgrUtils Array#together_reduce](#arraytogether_reduceor-treduce-together_inject-tinject) |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
|
@@ -435,6 +436,39 @@ print ret # => [[1, 2, 3, 4, 5, nil, 55, 66], [5, 6, 7, 8, 9, nil, 55, 66]],
|
|
435
436
|
|
436
437
|
[back to list](#list)
|
437
438
|
|
439
|
+
### Array#together_last
|
440
|
+
together_last has alias :tlast
|
441
|
+
|
442
|
+
no args case
|
443
|
+
~~~ruby
|
444
|
+
lists = [[*1..5], [*6..10]]
|
445
|
+
ret = lists.together_last
|
446
|
+
print ret # => [5, 10]
|
447
|
+
~~~
|
448
|
+
|
449
|
+
has args 2 case
|
450
|
+
~~~ruby
|
451
|
+
lists = [[*1..5], [*6..10]]
|
452
|
+
ret = lists.together_last 2
|
453
|
+
print ret # => [[4, 5], [9, 10]]
|
454
|
+
~~~
|
455
|
+
|
456
|
+
has args 0 case
|
457
|
+
~~~ruby
|
458
|
+
lists = [[*1..5], [*6..10]]
|
459
|
+
ret = lists.together_last 0
|
460
|
+
print ret # => [[], []]
|
461
|
+
~~~
|
462
|
+
|
463
|
+
has args over size case
|
464
|
+
~~~ruby
|
465
|
+
lists = [[*1..5], [*6..10]]
|
466
|
+
ret = lists.together_last 6
|
467
|
+
print ret # => [[*1..5], [*6..10]]
|
468
|
+
~~~
|
469
|
+
|
470
|
+
[back to list](#list)
|
471
|
+
|
438
472
|
### Array#together_map(or tmap, together_collect, tcollect)
|
439
473
|
~~~ruby
|
440
474
|
require 'tbpgr_utils'
|
@@ -1215,6 +1249,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
1215
1249
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
1216
1250
|
|
1217
1251
|
## History
|
1252
|
+
* version 0.0.34 : add Array#together_last(alias tlast).
|
1218
1253
|
* version 0.0.33 : add Array#together_shift(alias tshift).
|
1219
1254
|
* version 0.0.32 : add Array#together_insert(alias tinsert).
|
1220
1255
|
* version 0.0.31 : add Array#together_index(alias tindex).
|
data/lib/open_classes/array.rb
CHANGED
@@ -1,610 +1,640 @@
|
|
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
|
-
# Arrays bulk delete_if.
|
315
|
-
#
|
316
|
-
# together_delete_if has alias :tdelete_if
|
317
|
-
#
|
318
|
-
# if delete_if target is exist. return self.
|
319
|
-
# lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
|
320
|
-
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
321
|
-
# print ret # => [[2, 4], [4, 8]]
|
322
|
-
#
|
323
|
-
# if delete_if target is not exist. return nil.
|
324
|
-
# lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
|
325
|
-
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
326
|
-
# print ret # => nil
|
327
|
-
def together_delete_if(&block)
|
328
|
-
if_not_contain_array_rails_type_error
|
329
|
-
have_deleted = false
|
330
|
-
first.each_with_index do |i_v, i|
|
331
|
-
eval_each_str = get_args_str_for_together i
|
332
|
-
is_delete = instance_eval "yield(#{eval_each_str})"
|
333
|
-
if is_delete
|
334
|
-
each { |e|e.delete_at i }
|
335
|
-
have_deleted = true
|
336
|
-
end
|
337
|
-
end
|
338
|
-
have_deleted ? self : nil
|
339
|
-
end
|
340
|
-
|
341
|
-
# Arrays bulk empty?.
|
342
|
-
#
|
343
|
-
# together_empty? has alias :tempty?
|
344
|
-
#
|
345
|
-
# empty case
|
346
|
-
# lists = [[], []]
|
347
|
-
# ret = lists.together_empty?
|
348
|
-
# print ret # => true
|
349
|
-
#
|
350
|
-
# not empty case
|
351
|
-
# lists = [[1], []]
|
352
|
-
# ret = lists.together_empty?
|
353
|
-
# print ret # => false
|
354
|
-
def together_empty?
|
355
|
-
if_not_contain_array_rails_type_error
|
356
|
-
is_empty = true
|
357
|
-
each { |list|is_empty = is_empty && list.empty? }
|
358
|
-
is_empty
|
359
|
-
end
|
360
|
-
|
361
|
-
# Arrays bulk fill.
|
362
|
-
#
|
363
|
-
# together_fill has alias :tfill
|
364
|
-
#
|
365
|
-
# not use block case
|
366
|
-
# lists = [[*1..5], [*6..10]]
|
367
|
-
# ret = lists.together_fill(99)
|
368
|
-
# print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]
|
369
|
-
#
|
370
|
-
# use block, no args case
|
371
|
-
# lists = [[*1..5], [*6..10]]
|
372
|
-
# ret = lists.together_fill { |i|(i + 1) + 1 }
|
373
|
-
# print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
|
374
|
-
#
|
375
|
-
# use block, has args case
|
376
|
-
# lists = [[*1..5], [*6..10]]
|
377
|
-
# ret = lists.together_fill(2) { |i|(i + 1) + 1 }
|
378
|
-
# print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]
|
379
|
-
def together_fill(fill_value = nil, &block)
|
380
|
-
if_not_contain_array_rails_type_error
|
381
|
-
if block
|
382
|
-
fill_value = 0 if fill_value.nil?
|
383
|
-
first.each_with_index do |i_v, i|
|
384
|
-
next if i < fill_value
|
385
|
-
each { |list|list[i] = yield(i) }
|
386
|
-
end
|
387
|
-
else
|
388
|
-
each { |list|list.fill fill_value }
|
389
|
-
end
|
390
|
-
self
|
391
|
-
end
|
392
|
-
|
393
|
-
# Arrays bulk first.
|
394
|
-
#
|
395
|
-
# together_first has alias :tfirst
|
396
|
-
#
|
397
|
-
# no args case
|
398
|
-
# lists = [[*1..5], [*6..10]]
|
399
|
-
# ret = lists.together_first
|
400
|
-
# print ret # => [1, 6]
|
401
|
-
#
|
402
|
-
# has args 2 case
|
403
|
-
# lists = [[*1..5], [*6..10]]
|
404
|
-
# ret = lists.together_first 2
|
405
|
-
# print ret # => [[1, 2], [6, 7]]
|
406
|
-
#
|
407
|
-
# has args 0 case
|
408
|
-
# lists = [[*1..5], [*6..10]]
|
409
|
-
# ret = lists.together_first 0
|
410
|
-
# print ret # => [[], []]
|
411
|
-
#
|
412
|
-
# has args over size case
|
413
|
-
# lists = [[*1..5], [*6..10]]
|
414
|
-
# ret = lists.together_first 6
|
415
|
-
# print ret # => [[*1..5], [*6..10]]
|
416
|
-
def together_first(index = nil)
|
417
|
-
if_not_contain_array_rails_type_error
|
418
|
-
each_return = index == 0 ? '[]' : index.nil? ? 'list.first' : 'list
|
419
|
-
reduce([]) { |ret, list|ret << eval(each_return, binding) }
|
420
|
-
end
|
421
|
-
|
422
|
-
# Arrays bulk
|
423
|
-
#
|
424
|
-
#
|
425
|
-
#
|
426
|
-
#
|
427
|
-
# lists = [[*1..5], [*
|
428
|
-
# ret = lists.
|
429
|
-
# print ret # =>
|
430
|
-
#
|
431
|
-
#
|
432
|
-
# lists = [[*1..5], [*
|
433
|
-
# ret = lists.
|
434
|
-
# print ret # =>
|
435
|
-
#
|
436
|
-
#
|
437
|
-
# lists = [[*1..5], [*
|
438
|
-
# ret = lists.
|
439
|
-
# print ret # =>
|
440
|
-
#
|
441
|
-
#
|
442
|
-
# lists = [[*1..5], [*
|
443
|
-
# ret = lists.
|
444
|
-
# print ret # => [
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
#
|
452
|
-
#
|
453
|
-
#
|
454
|
-
#
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
#
|
462
|
-
#
|
463
|
-
#
|
464
|
-
#
|
465
|
-
# both
|
466
|
-
# lists = [[*1..5], [*5..9]]
|
467
|
-
# ret = lists.
|
468
|
-
# print ret # =>
|
469
|
-
#
|
470
|
-
#
|
471
|
-
# lists = [[*1..5], [*5..9]]
|
472
|
-
# ret = lists.
|
473
|
-
# print ret # => [
|
474
|
-
#
|
475
|
-
#
|
476
|
-
# lists = [[*1..5], [*5..9]]
|
477
|
-
# ret = lists.
|
478
|
-
# print ret # => [
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
#
|
491
|
-
#
|
492
|
-
#
|
493
|
-
#
|
494
|
-
#
|
495
|
-
#
|
496
|
-
#
|
497
|
-
#
|
498
|
-
#
|
499
|
-
#
|
500
|
-
#
|
501
|
-
#
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
#
|
518
|
-
#
|
519
|
-
#
|
520
|
-
# ret
|
521
|
-
#
|
522
|
-
#
|
523
|
-
#
|
524
|
-
#
|
525
|
-
#
|
526
|
-
#
|
527
|
-
#
|
528
|
-
#
|
529
|
-
#
|
530
|
-
#
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
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
|
+
# Arrays bulk delete_if.
|
315
|
+
#
|
316
|
+
# together_delete_if has alias :tdelete_if
|
317
|
+
#
|
318
|
+
# if delete_if target is exist. return self.
|
319
|
+
# lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
|
320
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
321
|
+
# print ret # => [[2, 4], [4, 8]]
|
322
|
+
#
|
323
|
+
# if delete_if target is not exist. return nil.
|
324
|
+
# lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
|
325
|
+
# ret = lists.together_delete_if {|first, second|(first + second).odd?}
|
326
|
+
# print ret # => nil
|
327
|
+
def together_delete_if(&block)
|
328
|
+
if_not_contain_array_rails_type_error
|
329
|
+
have_deleted = false
|
330
|
+
first.each_with_index do |i_v, i|
|
331
|
+
eval_each_str = get_args_str_for_together i
|
332
|
+
is_delete = instance_eval "yield(#{eval_each_str})"
|
333
|
+
if is_delete
|
334
|
+
each { |e|e.delete_at i }
|
335
|
+
have_deleted = true
|
336
|
+
end
|
337
|
+
end
|
338
|
+
have_deleted ? self : nil
|
339
|
+
end
|
340
|
+
|
341
|
+
# Arrays bulk empty?.
|
342
|
+
#
|
343
|
+
# together_empty? has alias :tempty?
|
344
|
+
#
|
345
|
+
# empty case
|
346
|
+
# lists = [[], []]
|
347
|
+
# ret = lists.together_empty?
|
348
|
+
# print ret # => true
|
349
|
+
#
|
350
|
+
# not empty case
|
351
|
+
# lists = [[1], []]
|
352
|
+
# ret = lists.together_empty?
|
353
|
+
# print ret # => false
|
354
|
+
def together_empty?
|
355
|
+
if_not_contain_array_rails_type_error
|
356
|
+
is_empty = true
|
357
|
+
each { |list|is_empty = is_empty && list.empty? }
|
358
|
+
is_empty
|
359
|
+
end
|
360
|
+
|
361
|
+
# Arrays bulk fill.
|
362
|
+
#
|
363
|
+
# together_fill has alias :tfill
|
364
|
+
#
|
365
|
+
# not use block case
|
366
|
+
# lists = [[*1..5], [*6..10]]
|
367
|
+
# ret = lists.together_fill(99)
|
368
|
+
# print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]
|
369
|
+
#
|
370
|
+
# use block, no args case
|
371
|
+
# lists = [[*1..5], [*6..10]]
|
372
|
+
# ret = lists.together_fill { |i|(i + 1) + 1 }
|
373
|
+
# print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]
|
374
|
+
#
|
375
|
+
# use block, has args case
|
376
|
+
# lists = [[*1..5], [*6..10]]
|
377
|
+
# ret = lists.together_fill(2) { |i|(i + 1) + 1 }
|
378
|
+
# print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]
|
379
|
+
def together_fill(fill_value = nil, &block)
|
380
|
+
if_not_contain_array_rails_type_error
|
381
|
+
if block
|
382
|
+
fill_value = 0 if fill_value.nil?
|
383
|
+
first.each_with_index do |i_v, i|
|
384
|
+
next if i < fill_value
|
385
|
+
each { |list|list[i] = yield(i) }
|
386
|
+
end
|
387
|
+
else
|
388
|
+
each { |list|list.fill fill_value }
|
389
|
+
end
|
390
|
+
self
|
391
|
+
end
|
392
|
+
|
393
|
+
# Arrays bulk first.
|
394
|
+
#
|
395
|
+
# together_first has alias :tfirst
|
396
|
+
#
|
397
|
+
# no args case
|
398
|
+
# lists = [[*1..5], [*6..10]]
|
399
|
+
# ret = lists.together_first
|
400
|
+
# print ret # => [1, 6]
|
401
|
+
#
|
402
|
+
# has args 2 case
|
403
|
+
# lists = [[*1..5], [*6..10]]
|
404
|
+
# ret = lists.together_first 2
|
405
|
+
# print ret # => [[1, 2], [6, 7]]
|
406
|
+
#
|
407
|
+
# has args 0 case
|
408
|
+
# lists = [[*1..5], [*6..10]]
|
409
|
+
# ret = lists.together_first 0
|
410
|
+
# print ret # => [[], []]
|
411
|
+
#
|
412
|
+
# has args over size case
|
413
|
+
# lists = [[*1..5], [*6..10]]
|
414
|
+
# ret = lists.together_first 6
|
415
|
+
# print ret # => [[*1..5], [*6..10]]
|
416
|
+
def together_first(index = nil)
|
417
|
+
if_not_contain_array_rails_type_error
|
418
|
+
each_return = index == 0 ? '[]' : index.nil? ? 'list.first' : 'list.first(index)'
|
419
|
+
reduce([]) { |ret, list|ret << eval(each_return, binding) }
|
420
|
+
end
|
421
|
+
|
422
|
+
# Arrays bulk last.
|
423
|
+
#
|
424
|
+
# together_last has alias :tlast
|
425
|
+
#
|
426
|
+
# no args case
|
427
|
+
# lists = [[*1..5], [*6..10]]
|
428
|
+
# ret = lists.together_last
|
429
|
+
# print ret # => [5, 10]
|
430
|
+
#
|
431
|
+
# has args 2 case
|
432
|
+
# lists = [[*1..5], [*6..10]]
|
433
|
+
# ret = lists.together_last 2
|
434
|
+
# print ret # => [[4, 5], [9, 10]]
|
435
|
+
#
|
436
|
+
# has args 0 case
|
437
|
+
# lists = [[*1..5], [*6..10]]
|
438
|
+
# ret = lists.together_last 0
|
439
|
+
# print ret # => [[], []]
|
440
|
+
#
|
441
|
+
# has args over size case
|
442
|
+
# lists = [[*1..5], [*6..10]]
|
443
|
+
# ret = lists.together_last 6
|
444
|
+
# print ret # => [[*1..5], [*6..10]]
|
445
|
+
def together_last(index = nil)
|
446
|
+
if_not_contain_array_rails_type_error
|
447
|
+
each_return = index == 0 ? '[]' : index.nil? ? 'list.last' : 'list.last(index)'
|
448
|
+
reduce([]) { |ret, list|ret << eval(each_return, binding) }
|
449
|
+
end
|
450
|
+
|
451
|
+
# Arrays bulk include?.
|
452
|
+
#
|
453
|
+
# together_include? has alias :tinclude?
|
454
|
+
#
|
455
|
+
# both include single ret case
|
456
|
+
# lists = [[*1..5], [*5..9]]
|
457
|
+
# ret = lists.together_include? 5
|
458
|
+
# print ret # => true
|
459
|
+
#
|
460
|
+
# one include single ret case
|
461
|
+
# lists = [[*1..5], [*5..9]]
|
462
|
+
# ret = lists.together_include? 9
|
463
|
+
# print ret # => true
|
464
|
+
#
|
465
|
+
# both not include single ret case
|
466
|
+
# lists = [[*1..5], [*5..9]]
|
467
|
+
# ret = lists.together_include? 10
|
468
|
+
# print ret # => false
|
469
|
+
#
|
470
|
+
# both include multi ret case
|
471
|
+
# lists = [[*1..5], [*5..9]]
|
472
|
+
# ret = lists.together_include? 5, true
|
473
|
+
# print ret # => [true, true]
|
474
|
+
#
|
475
|
+
# one include multi ret case
|
476
|
+
# lists = [[*1..5], [*5..9]]
|
477
|
+
# ret = lists.together_include? 9, true
|
478
|
+
# print ret # => [false, true]
|
479
|
+
#
|
480
|
+
# both not include multi ret case
|
481
|
+
# lists = [[*1..5], [*5..9]]
|
482
|
+
# ret = lists.together_include? 10, true
|
483
|
+
# print ret # => [false, false]
|
484
|
+
def together_include?(value, is_multi = false)
|
485
|
+
if_not_contain_array_rails_type_error
|
486
|
+
return reduce([]) { |ret, list|ret << list.include?(value) } if is_multi
|
487
|
+
reduce(false) { |ret, list|ret = ret || list.include?(value) }
|
488
|
+
end
|
489
|
+
|
490
|
+
# Arrays bulk index.
|
491
|
+
#
|
492
|
+
# together_index has alias :tindex
|
493
|
+
#
|
494
|
+
# both index exist case
|
495
|
+
# lists = [[*1..5], [*5..9]]
|
496
|
+
# ret = lists.together_index 5
|
497
|
+
# print ret # => [4, 0]
|
498
|
+
#
|
499
|
+
# one include single ret case
|
500
|
+
# lists = [[*1..5], [*5..9]]
|
501
|
+
# ret = lists.together_index 4
|
502
|
+
# print ret # => [3, nil]
|
503
|
+
#
|
504
|
+
# both not include single ret case
|
505
|
+
# lists = [[*1..5], [*5..9]]
|
506
|
+
# ret = lists.together_index 10
|
507
|
+
# print ret # => [nil, nil]
|
508
|
+
def together_index(value)
|
509
|
+
if_not_contain_array_rails_type_error
|
510
|
+
reduce([]) { |ret, list|ret << list.index(value) }
|
511
|
+
end
|
512
|
+
|
513
|
+
# Arrays bulk insert.
|
514
|
+
#
|
515
|
+
# together_insert has alias :tinsert
|
516
|
+
#
|
517
|
+
# both insert exist case
|
518
|
+
# lists = [[*1..5], [*5..9]]
|
519
|
+
# ret = lists.together_insert(1, 55, 66)
|
520
|
+
# print ret # => [[1, 55, 66, 2, 3, 4, 5], [5, 55, 66, 6, 7, 8, 9]]
|
521
|
+
#
|
522
|
+
# both insert exist and minus index case
|
523
|
+
# lists = [[*1..5], [*5..9]]
|
524
|
+
# ret = lists.together_insert(-2, 55, 66)
|
525
|
+
# print ret # => [[1, 2, 3, 4, 55, 66, 5], [5, 6, 7, 8, 55, 66, 9]]
|
526
|
+
#
|
527
|
+
# both insert exist case
|
528
|
+
# lists = [[*1..5], [*5..9]]
|
529
|
+
# ret = lists.together_insert(6, 55, 66)
|
530
|
+
# print ret # => [[1, 2, 3, 4, 5, nil, 55, 66], [5, 6, 7, 8, 9, nil, 55, 66]],
|
531
|
+
def together_insert(index, *args)
|
532
|
+
if_not_contain_array_rails_type_error
|
533
|
+
each { |list|list.insert(index, *args) }
|
534
|
+
end
|
535
|
+
|
536
|
+
|
537
|
+
# Arrays bulk shift.
|
538
|
+
#
|
539
|
+
# together_shift has alias :tshift
|
540
|
+
#
|
541
|
+
# not empty case
|
542
|
+
# lists = [[1, 2], [5, 6]]
|
543
|
+
# ret = lists.together_shift
|
544
|
+
# print ret # => [1, 5]
|
545
|
+
# print lists # => [2, 6]
|
546
|
+
#
|
547
|
+
# empty case
|
548
|
+
# lists = [[], []]
|
549
|
+
# ret = lists.together_shift
|
550
|
+
# print ret # => [nil, nil]
|
551
|
+
# print lists # => [[], []]
|
552
|
+
#
|
553
|
+
# not empty case with args
|
554
|
+
# lists = [[1, 2], [5, 6]]
|
555
|
+
# ret = lists.together_shift 2
|
556
|
+
# print ret # => [[1, 2], [5, 6]]
|
557
|
+
# print lists # => [[], []]
|
558
|
+
#
|
559
|
+
# not empty case with args
|
560
|
+
# lists = [[], []]
|
561
|
+
# ret = lists.together_shift 2
|
562
|
+
# print ret # => [[], []]
|
563
|
+
# print lists # => [[], []]
|
564
|
+
def together_shift(count = nil)
|
565
|
+
if_not_contain_array_rails_type_error
|
566
|
+
if count.nil?
|
567
|
+
reduce([]) { |ret, list|ret << list.shift }
|
568
|
+
else
|
569
|
+
reduce([]) { |ret, list|ret << list.shift(count) }
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
573
|
+
private
|
574
|
+
|
575
|
+
def if_not_contain_array_rails_type_error
|
576
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
577
|
+
end
|
578
|
+
|
579
|
+
def get_args_for_together(i)
|
580
|
+
eval_each = []
|
581
|
+
each_with_index { |j_v, j|eval_each << "self[#{j}][#{i}]" }
|
582
|
+
eval_each
|
583
|
+
end
|
584
|
+
|
585
|
+
def get_args_str_for_together(i, with_index = false)
|
586
|
+
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
587
|
+
each_eval.join(',')
|
588
|
+
end
|
589
|
+
|
590
|
+
def set_together_each_return_map(ret, each_ret, index)
|
591
|
+
if together_return_multi?(each_ret)
|
592
|
+
size.times { |i|ret << [] } if index == 0
|
593
|
+
(0..(size - 1)).each { |i|ret[i] << each_ret[i] }
|
594
|
+
else
|
595
|
+
ret << each_ret
|
596
|
+
end
|
597
|
+
ret
|
598
|
+
end
|
599
|
+
|
600
|
+
def set_together_each_return_select(ret, each_ret, index)
|
601
|
+
unless together_return_multi?(each_ret)
|
602
|
+
tmp_each_ret = []
|
603
|
+
size.times { tmp_each_ret << each_ret }
|
604
|
+
each_ret = tmp_each_ret
|
605
|
+
end
|
606
|
+
size.times { |i|ret << [] } if index == 0
|
607
|
+
(0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] }
|
608
|
+
ret
|
609
|
+
end
|
610
|
+
|
611
|
+
def initial_memo(init)
|
612
|
+
return init unless init.nil?
|
613
|
+
first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
|
614
|
+
end
|
615
|
+
|
616
|
+
def together_return_multi?(list)
|
617
|
+
(list.class == Array && list.size == size).to_bool
|
618
|
+
end
|
619
|
+
|
620
|
+
alias_method :tconcat, :together_concat
|
621
|
+
alias_method :tat, :together_at
|
622
|
+
alias_method :tclear, :together_clear
|
623
|
+
alias_method :tcompact, :together_compact
|
624
|
+
alias_method :tcompact!, :together_compact!
|
625
|
+
alias_method :tdelete, :together_delete
|
626
|
+
alias_method :tdelete_at, :together_delete_at
|
627
|
+
alias_method :tdelete_if, :together_delete_if
|
628
|
+
alias_method :tempty?, :together_empty?
|
629
|
+
alias_method :tfill, :together_fill
|
630
|
+
alias_method :tfirst, :together_first
|
631
|
+
alias_method :tlast, :together_last
|
632
|
+
alias_method :tinclude?, :together_include?
|
633
|
+
alias_method :tindex, :together_index
|
634
|
+
alias_method :tinsert, :together_insert
|
635
|
+
alias_method :tshift, :together_shift
|
636
|
+
alias_methods [:together_collect, :tmap, :tcollect], :together_map
|
637
|
+
alias_methods [:together_collect!, :tmap!, :tcollect!], :together_map!
|
638
|
+
alias_methods [:together_find_all, :tselect, :tfindall], :together_select
|
639
|
+
alias_methods [:together_inject, :treduce, :tinject], :together_reduce
|
640
|
+
end
|