tbpgr_utils 0.0.36 → 0.0.37

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/README.md +40 -1
  2. data/lib/open_classes/array/together.rb +23 -0
  3. data/lib/open_classes/array/together_at.rb +29 -0
  4. data/lib/open_classes/array/together_clear.rb +24 -0
  5. data/lib/open_classes/array/together_compact.rb +44 -0
  6. data/lib/open_classes/array/together_concat.rb +25 -0
  7. data/lib/open_classes/array/together_delete.rb +46 -0
  8. data/lib/open_classes/array/together_delete_at.rb +43 -0
  9. data/lib/open_classes/array/together_delete_if.rb +38 -0
  10. data/lib/open_classes/array/together_empty.rb +31 -0
  11. data/lib/open_classes/array/together_fill.rb +43 -0
  12. data/lib/open_classes/array/together_first.rb +40 -0
  13. data/lib/open_classes/array/together_helper.rb +25 -0
  14. data/lib/open_classes/array/together_include.rb +50 -0
  15. data/lib/open_classes/array/together_index.rb +34 -0
  16. data/lib/open_classes/array/together_insert.rb +34 -0
  17. data/lib/open_classes/array/together_last.rb +40 -0
  18. data/lib/open_classes/array/together_map.rb +87 -0
  19. data/lib/open_classes/array/together_pop.rb +47 -0
  20. data/lib/open_classes/array/together_reduce.rb +59 -0
  21. data/lib/open_classes/array/together_reverse.rb +53 -0
  22. data/lib/open_classes/array/together_sample.rb +49 -0
  23. data/lib/open_classes/array/together_select.rb +50 -0
  24. data/lib/open_classes/array/together_shift.rb +47 -0
  25. data/lib/open_classes/array/together_with_index.rb +24 -0
  26. data/lib/open_classes/array.rb +23 -718
  27. data/lib/tbpgr_utils/version.rb +1 -1
  28. data/spec/open_classes/array/together_at_spec.rb +61 -0
  29. data/spec/open_classes/array/together_clear_spec.rb +51 -0
  30. data/spec/open_classes/array/together_compact_spec.rb +103 -0
  31. data/spec/open_classes/array/together_concat_spec.rb +53 -0
  32. data/spec/open_classes/array/together_delete_at_spec.rb +78 -0
  33. data/spec/open_classes/array/together_delete_if_spec.rb +61 -0
  34. data/spec/open_classes/array/together_delete_spec.rb +80 -0
  35. data/spec/open_classes/array/together_empty_spec.rb +58 -0
  36. data/spec/open_classes/array/together_fill_spec.rb +79 -0
  37. data/spec/open_classes/array/together_first_spec.rb +84 -0
  38. data/spec/open_classes/array/together_include_spec.rb +100 -0
  39. data/spec/open_classes/array/together_index_spec.rb +69 -0
  40. data/spec/open_classes/array/together_insert_spec.rb +65 -0
  41. data/spec/open_classes/array/together_last_spec.rb +84 -0
  42. data/spec/open_classes/array/together_map_spec.rb +171 -0
  43. data/spec/open_classes/array/together_pop_spec.rb +105 -0
  44. data/spec/open_classes/array/together_reduce_spec.rb +100 -0
  45. data/spec/open_classes/array/together_reverse_spec.rb +119 -0
  46. data/spec/open_classes/array/together_select_spec.rb +76 -0
  47. data/spec/open_classes/array/together_shift_spec.rb +105 -0
  48. data/spec/open_classes/array/together_spec.rb +51 -0
  49. data/spec/open_classes/array/together_with_index_spec.rb +51 -0
  50. data/spec/open_classes/together_sample_spec.rb +122 -0
  51. data/spec/spec_helper.rb +1 -1
  52. metadata +82 -14
  53. data/spec/open_classes/array_spec.rb +0 -1699
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays together map.
11
+ #
12
+ # together_map has aliases [:tmap, :together_collect, :tcollect]
13
+ #
14
+ # if you want to single Array return
15
+ # alpha = %w{one two three}
16
+ # numbers = %w{1 2 3}
17
+ # print [alpha, numbers].together_map do |first, second|
18
+ # "#{first}:#{second}\n"
19
+ # end # => output one:1, two:2, three:3
20
+ #
21
+ # if you want to multi Array return
22
+ # alpha = %w{one two three}
23
+ # numbers = %w{1 2 3}
24
+ # print [alpha, numbers].together_map do |first, second|
25
+ # ["#{first}:#{second}", "#{second}:#{first}"]
26
+ # end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
27
+ def together_map
28
+ if_not_contain_array_rails_type_error
29
+ ret = []
30
+ first.each_with_index do |i_v, i|
31
+ eval_each_str = get_args_str_for_together i
32
+ each_ret = instance_eval "yield(#{eval_each_str})"
33
+ ret = set_together_each_return_map(ret, each_ret, i)
34
+ end
35
+ ret
36
+ end
37
+
38
+ # Arrays together map!.
39
+ #
40
+ # together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
41
+ #
42
+ # if you want to single Array return
43
+ # alpha = %w{one two three}
44
+ # numbers = %w{1 2 3}
45
+ # ary = [alpha, numbers]
46
+ # ret = ary.together_map! do |first, second|
47
+ # "#{first}:#{second}"
48
+ # end
49
+ # print ret # => output ['one:1', 'two:2', 'three:3']
50
+ # print ary # => output ['one:1', 'two:2', 'three:3']
51
+ #
52
+ # if you want to multi Array return
53
+ # alpha = %w{one two three}
54
+ # numbers = %w{1 2 3}
55
+ # ary = [alpha, numbers]
56
+ # ret = ary.together_map! do |first, second|
57
+ # ["#{first}:#{second}", "#{second}:#{first}"]
58
+ # end
59
+ # print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
60
+ # print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
61
+ def together_map!
62
+ if_not_contain_array_rails_type_error
63
+ ret = []
64
+ first.each_with_index do |i_v, i|
65
+ eval_each_str = get_args_str_for_together i
66
+ each_ret = instance_eval "yield(#{eval_each_str})"
67
+ ret = set_together_each_return_map(ret, each_ret, i)
68
+ end
69
+ clear
70
+ ret.each { |v|self << v }
71
+ end
72
+
73
+ private
74
+
75
+ def set_together_each_return_map(ret, each_ret, index)
76
+ if together_return_multi?(each_ret)
77
+ size.times { |i|ret << [] } if index == 0
78
+ (0..(size - 1)).each { |i|ret[i] << each_ret[i] }
79
+ else
80
+ ret << each_ret
81
+ end
82
+ ret
83
+ end
84
+
85
+ alias_methods [:together_collect, :tmap, :tcollect], :together_map
86
+ alias_methods [:together_collect!, :tmap!, :tcollect!], :together_map!
87
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays bulk pop.
11
+ #
12
+ # together_pop has alias :tpop
13
+ #
14
+ # not empty case
15
+ # lists = [[1, 2], [5, 6]]
16
+ # ret = lists.together_pop
17
+ # print ret # => [2, 6]
18
+ # print lists # => [1, 5]
19
+ #
20
+ # empty case
21
+ # lists = [[], []]
22
+ # ret = lists.together_pop
23
+ # print ret # => [nil, nil]
24
+ # print lists # => [[], []]
25
+ #
26
+ # not empty case with args
27
+ # lists = [[1, 2], [5, 6]]
28
+ # ret = lists.together_pop 2
29
+ # print ret # => [[1, 2], [5, 6]]
30
+ # print lists # => [[], []]
31
+ #
32
+ # not empty case with args
33
+ # lists = [[], []]
34
+ # ret = lists.together_pop 2
35
+ # print ret # => [[], []]
36
+ # print lists # => [[], []]
37
+ def together_pop(count = nil)
38
+ if_not_contain_array_rails_type_error
39
+ if count.nil?
40
+ reduce([]) { |ret, list|ret << list.pop }
41
+ else
42
+ reduce([]) { |ret, list|ret << list.pop(count) }
43
+ end
44
+ end
45
+
46
+ alias_method :tpop, :together_pop
47
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays loop together reduce.
11
+ #
12
+ # together_reduce has aliases [:treduce, :together_inject, :tinject]
13
+ #
14
+ # if you want to single return
15
+ # firsts = [1, 2, 3, 4]
16
+ # seconds = [4, 2, 3, 1]
17
+ # ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
18
+ # print ret # => output 20
19
+ #
20
+ # if you want to single return with init value
21
+ # firsts = [1, 2, 3, 4]
22
+ # seconds = [4, 2, 3, 1]
23
+ # ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
24
+ # print ret # => output 30
25
+ #
26
+ # if you want to single return with init string value
27
+ # firsts = %w{a b c}
28
+ # seconds = %w{1 2 3}
29
+ # ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
30
+ # print ret # => output 'start-a1b2c3'
31
+ #
32
+ # if you want to single return with init Array value
33
+ # firsts = [1, 2, 3, 4]
34
+ # seconds = [4, 2, 3, 1]
35
+ # ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
36
+ # print ret # => output [5, 4, 6, 5]
37
+ #
38
+ # if you want to single return with init Hash value
39
+ # firsts = [1, 2, 3, 4]
40
+ # seconds = [4, 2, 3, 1]
41
+ # ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
42
+ # print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
43
+ def together_reduce(init = nil)
44
+ if_not_contain_array_rails_type_error
45
+ memo = initial_memo init
46
+ first.each_with_index do |i_v, i|
47
+ eval_each_str = get_args_str_for_together i
48
+ memo = instance_eval "yield(memo, #{eval_each_str})"
49
+ end
50
+ memo
51
+ end
52
+
53
+ def initial_memo(init)
54
+ return init unless init.nil?
55
+ first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
56
+ end
57
+
58
+ alias_methods [:together_inject, :treduce, :tinject], :together_reduce
59
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays bulk reverse.
11
+ #
12
+ # together_reverse has alias :treverse
13
+ #
14
+ # not empty case
15
+ # lists = [[1, 2], [5, 6]]
16
+ # ret = lists.together_reverse
17
+ # print ret # => [[2, 1], [6, 5]]
18
+ # print lists # => [[1, 2], [5, 6]]
19
+ #
20
+ # one empty case
21
+ # lists = [[1, 2], []]
22
+ # ret = lists.together_reverse
23
+ # print ret # => [[2, 1], []]
24
+ # print lists # => [[1, 2], []]
25
+ def together_reverse
26
+ if_not_contain_array_rails_type_error
27
+ reduce([]) { |ret, list|ret << list.reverse }
28
+ end
29
+
30
+
31
+ # Arrays bulk reverse!.
32
+ #
33
+ # together_reverse! has alias :treverse!
34
+ #
35
+ # not empty case
36
+ # lists = [[1, 2], [5, 6]]
37
+ # ret = lists.together_reverse!
38
+ # print ret # => [[2, 1], [6, 5]]
39
+ # print lists # => [[2, 1], [6, 5]]
40
+ #
41
+ # one empty case
42
+ # lists = [[1, 2], []]
43
+ # ret = lists.together_reverse!
44
+ # print ret # => [[2, 1], []]
45
+ # print lists # => [[2, 1], []]
46
+ def together_reverse!
47
+ if_not_contain_array_rails_type_error
48
+ reduce([]) { |ret, list|ret << list.reverse! }
49
+ end
50
+
51
+ alias_method :treverse, :together_reverse
52
+ alias_method :treverse!, :together_reverse!
53
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays bulk sample.
11
+ #
12
+ # together_sample has alias :tsample
13
+ #
14
+ # not empty case
15
+ # lists = [[1, 2], [5, 6]]
16
+ # ret = lists.together_sample
17
+ # print ret # => [1 or 2, 5 or 6]
18
+ #
19
+ # empty case
20
+ # lists = [[], []]
21
+ # ret = lists.together_sample
22
+ # print ret # => [nil, nil]
23
+ #
24
+ # not empty case with args
25
+ # lists = [[1, 2], [5, 6]]
26
+ # ret = lists.together_sample 2
27
+ # print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
28
+ #
29
+ # not empty case with args
30
+ # lists = [[], []]
31
+ # ret = lists.together_sample 2
32
+ # print ret # => [[], []]
33
+ #
34
+ # not empty, over size case with args
35
+ # lists = [[1, 2], [5, 6]]
36
+ # ret = lists.together_sample 3
37
+ # print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]
38
+ #
39
+ def together_sample(count = nil)
40
+ if_not_contain_array_rails_type_error
41
+ if count.nil?
42
+ reduce([]) { |ret, list|ret << list.sample }
43
+ else
44
+ reduce([]) { |ret, list|ret << list.sample(count) }
45
+ end
46
+ end
47
+
48
+ alias_method :tsample, :together_sample
49
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays loop together select.
11
+ #
12
+ # together_select has aliases [:tselect, :together_find_all, :tfindall]
13
+ #
14
+ # if you want to single Array return
15
+ # firsts = [1, 2, 3, 4]
16
+ # seconds = [4, 2, 3, 1]
17
+ # ret = [firsts, seconds].together_select{|first, second|first == second}
18
+ # print ret # => output [[2, 3], [2, 3]]
19
+ #
20
+ # if you want to multi Array return
21
+ # firsts = [1, 2, 3, 4]
22
+ # seconds = [4, 2, 3, 1]
23
+ # ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
24
+ # print ret # => output [[1, 3], [4, 2]]
25
+ def together_select
26
+ if_not_contain_array_rails_type_error
27
+ ret = []
28
+ first.each_with_index do |i_v, i|
29
+ eval_each_str = get_args_str_for_together i
30
+ each_ret = instance_eval "yield(#{eval_each_str})"
31
+ ret = set_together_each_return_select(ret, each_ret, i)
32
+ end
33
+ ret
34
+ end
35
+
36
+ private
37
+
38
+ def set_together_each_return_select(ret, each_ret, index)
39
+ unless together_return_multi?(each_ret)
40
+ tmp_each_ret = []
41
+ size.times { tmp_each_ret << each_ret }
42
+ each_ret = tmp_each_ret
43
+ end
44
+ size.times { |i|ret << [] } if index == 0
45
+ (0..(size - 1)).each { |i|ret[i] << self[i][index] if each_ret[i] }
46
+ ret
47
+ end
48
+
49
+ alias_methods [:together_find_all, :tselect, :tfindall], :together_select
50
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays bulk shift.
11
+ #
12
+ # together_shift has alias :tshift
13
+ #
14
+ # not empty case
15
+ # lists = [[1, 2], [5, 6]]
16
+ # ret = lists.together_shift
17
+ # print ret # => [1, 5]
18
+ # print lists # => [2, 6]
19
+ #
20
+ # empty case
21
+ # lists = [[], []]
22
+ # ret = lists.together_shift
23
+ # print ret # => [nil, nil]
24
+ # print lists # => [[], []]
25
+ #
26
+ # not empty case with args
27
+ # lists = [[1, 2], [5, 6]]
28
+ # ret = lists.together_shift 2
29
+ # print ret # => [[1, 2], [5, 6]]
30
+ # print lists # => [[], []]
31
+ #
32
+ # not empty case with args
33
+ # lists = [[], []]
34
+ # ret = lists.together_shift 2
35
+ # print ret # => [[], []]
36
+ # print lists # => [[], []]
37
+ def together_shift(count = nil)
38
+ if_not_contain_array_rails_type_error
39
+ if count.nil?
40
+ reduce([]) { |ret, list|ret << list.shift }
41
+ else
42
+ reduce([]) { |ret, list|ret << list.shift(count) }
43
+ end
44
+ end
45
+
46
+ alias_method :tshift, :together_shift
47
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/object'
3
+ require 'open_classes/module'
4
+ require 'open_classes/array/together_helper'
5
+
6
+ # Array
7
+ class Array
8
+ include TogetherHelper
9
+
10
+ # Arrays loop together with index.
11
+ #
12
+ # alpha = %w{one two three}
13
+ # numbers = %w{1 2 3}
14
+ # [alpha, numbers].together_with_index do |first, second, index|
15
+ # print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
16
+ # end
17
+ def together_with_index
18
+ if_not_contain_array_rails_type_error
19
+ first.each_with_index do |i_v, i|
20
+ eval_each_str = get_args_str_for_together i, true
21
+ instance_eval "yield(#{eval_each_str})"
22
+ end
23
+ end
24
+ end