tbpgr_utils 0.0.16 → 0.0.17

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/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
  gem "rspec", "~> 2.14.1"
5
5
  gem "activesupport", "~> 4.0.1"
6
+ gem "simplecov", "~> 0.8.2"
data/README.md CHANGED
@@ -22,6 +22,7 @@ Or install it yourself as:
22
22
  |:----------- |:------------ |
23
23
  |TbpgrUtils Array#together |loop all arrays by block |
24
24
  |TbpgrUtils Array#together_map |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
25
+ |TbpgrUtils Array#together_reduce |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
25
26
  |TbpgrUtils Array#together_select |together version of Enumerable#select. together_select has aliases [:tselect, :together_find_all, :tfindall]|
26
27
  |TbpgrUtils Array#together_with_index |loop all arrays by block with index |
27
28
  |AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessor + initializer |
@@ -74,6 +75,52 @@ ret = [alpha, numbers].together_map {|first, second|[["#{first}:ret"], ["#{secon
74
75
  print ret # => output [["one:ret", "two:ret", "three:ret"],["1:ret", "2:ret", "3:ret"]]
75
76
  ~~~
76
77
 
78
+ ### Array#together_reduce(or :treduce, :together_inject, :tinject)
79
+ * if you want to single return
80
+
81
+ ~~~ruby
82
+ firsts = [1, 2, 3, 4]
83
+ seconds = [4, 2, 3, 1]
84
+ ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
85
+ print ret # => output 20
86
+ ~~~
87
+
88
+ * if you want to single return with init value
89
+
90
+ ~~~ruby
91
+ firsts = [1, 2, 3, 4]
92
+ seconds = [4, 2, 3, 1]
93
+ ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
94
+ print ret # => output 30
95
+ ~~~
96
+
97
+ * if you want to single return with init string value
98
+
99
+ ~~~ruby
100
+ firsts = %w{a b c}
101
+ seconds = %w{1 2 3}
102
+ ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
103
+ print ret # => output 'start-a1b2c3'
104
+ ~~~
105
+
106
+ * if you want to single return with init Array value
107
+
108
+ ~~~ruby
109
+ firsts = [1, 2, 3, 4]
110
+ seconds = [4, 2, 3, 1]
111
+ ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
112
+ print ret # => output [5, 4, 6, 5]
113
+ ~~~
114
+
115
+ * if you want to single return with init Hash value
116
+
117
+ ~~~ruby
118
+ firsts = [1, 2, 3, 4]
119
+ seconds = [4, 2, 3, 1]
120
+ ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
121
+ print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
122
+ ~~~
123
+
77
124
  ### Array#together_select(or tselect, together_find_all, tfindall)
78
125
  ~~~ruby
79
126
  require 'tbpgr_utils'
@@ -635,6 +682,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
635
682
  https://github.com/tbpgr/tbpgr_utils_snippets
636
683
 
637
684
  ## History
685
+ * version 0.0.17 : add Array#together_reduce(or :treduce, :together_inject, :tinject)
638
686
  * version 0.0.16 : add Array#together_select(or tselect, together_find_all, tfindall)
639
687
  * version 0.0.15 : add Module.alias_methods
640
688
  * version 0.0.14 : add Array#together_map(aliases => [tmap, together_collect, tcollect])
@@ -64,6 +64,8 @@ class Array
64
64
 
65
65
  # Arrays loop together select.
66
66
  #
67
+ # together_select has aliases [:tselect, :together_find_all, :tfindall]
68
+ #
67
69
  # if you want to single Array return
68
70
  # firsts = [1, 2, 3, 4]
69
71
  # seconds = [4, 2, 3, 1]
@@ -73,7 +75,7 @@ class Array
73
75
  # if you want to multi Array return
74
76
  # firsts = [1, 2, 3, 4]
75
77
  # seconds = [4, 2, 3, 1]
76
- # ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?}
78
+ # ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
77
79
  # print ret # => output [[1, 3], [4, 2]]
78
80
  def together_select
79
81
  if_not_contain_array_rails_type_error
@@ -86,8 +88,52 @@ class Array
86
88
  ret
87
89
  end
88
90
 
91
+ # Arrays loop together reduce.
92
+ #
93
+ # together_reduce has aliases [:treduce, :together_inject, :tinject]
94
+ #
95
+ # if you want to single return
96
+ # firsts = [1, 2, 3, 4]
97
+ # seconds = [4, 2, 3, 1]
98
+ # ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
99
+ # print ret # => output 20
100
+ #
101
+ # if you want to single return with init value
102
+ # firsts = [1, 2, 3, 4]
103
+ # seconds = [4, 2, 3, 1]
104
+ # ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
105
+ # print ret # => output 30
106
+ #
107
+ # if you want to single return with init string value
108
+ # firsts = %w{a b c}
109
+ # seconds = %w{1 2 3}
110
+ # ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
111
+ # print ret # => output 'start-a1b2c3'
112
+ #
113
+ # if you want to single return with init Array value
114
+ # firsts = [1, 2, 3, 4]
115
+ # seconds = [4, 2, 3, 1]
116
+ # ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
117
+ # print ret # => output [5, 4, 6, 5]
118
+ #
119
+ # if you want to single return with init Hash value
120
+ # firsts = [1, 2, 3, 4]
121
+ # seconds = [4, 2, 3, 1]
122
+ # ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
123
+ # print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
124
+ def together_reduce(init = nil)
125
+ if_not_contain_array_rails_type_error
126
+ memo = initial_memo init
127
+ first.each_with_index do |i_v, i|
128
+ eval_each_str = get_args_str_for_together i
129
+ memo = instance_eval "yield(memo, #{eval_each_str})"
130
+ end
131
+ memo
132
+ end
133
+
89
134
  alias_methods [:together_collect, :tmap, :tcollect], :together_map
90
135
  alias_methods [:together_find_all, :tselect, :tfindall], :together_select
136
+ alias_methods [:together_inject, :treduce, :tinject], :together_reduce
91
137
 
92
138
  private
93
139
 
@@ -127,6 +173,11 @@ class Array
127
173
  ret
128
174
  end
129
175
 
176
+ def initial_memo(init)
177
+ return init unless init.nil?
178
+ first.first.is_a?(Numeric) ? 0 : first.first.is_a?(String) ? '' : nil
179
+ end
180
+
130
181
  def together_return_multi?(list)
131
182
  (list.class == Array && list.size == size).to_bool
132
183
  end
@@ -1,65 +1,65 @@
1
- # encoding: utf-8
2
-
3
- # Object
4
- class Object
5
- # Check boolean type
6
- #
7
- # boolean? true # => true
8
- # boolean? false # => true
9
- # boolean? nil # => false
10
- # boolean? 'true' # => false
11
- # boolean? 'false' # => false
12
- # boolean? '' # => false
13
- def boolean?
14
- self.is_a?(TrueClass) || self.is_a?(FalseClass)
15
- end
16
-
17
- # Get self define methods.
18
- #
19
- # class SampleClass < String
20
- # def public_hello
21
- # "public hello"
22
- # end
23
- #
24
- # protected
25
- #
26
- # def protected_hello
27
- # "protected hello"
28
- # end
29
- #
30
- # private
31
- #
32
- # def private_hello
33
- # "private hello"
34
- # end
35
- # end
36
- #
37
- # SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
38
- def my_methods
39
- public_methods(false) + protected_methods(false) + private_methods(false)
40
- end
41
-
42
- # If self match any one of args, return true.
43
- #
44
- # "hoge".any_of? %w{hoge hige} # => true
45
- # "hige".any_of? %w{hoge hige} # => true
46
- # "hege".any_of? %w{hoge hige} # => false
47
- def any_of?(*args)
48
- args.each { |value|return true if self == value }
49
- false
50
- end
51
-
52
- # you get bool value
53
- #
54
- # true.to_bool # => true
55
- # false.to_bool # => false
56
- # 0.to_bool # => true
57
- # 1.to_bool # => true
58
- # ''.to_bool # => true
59
- # 'true'.to_bool # => true
60
- # 'false'.to_bool # => true
61
- # nil.to_bool # => false
62
- def to_bool
63
- !!self
64
- end
65
- end
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # Check boolean type
6
+ #
7
+ # boolean? true # => true
8
+ # boolean? false # => true
9
+ # boolean? nil # => false
10
+ # boolean? 'true' # => false
11
+ # boolean? 'false' # => false
12
+ # boolean? '' # => false
13
+ def boolean?
14
+ self.is_a?(TrueClass) || self.is_a?(FalseClass)
15
+ end
16
+
17
+ # Get self define methods.
18
+ #
19
+ # class SampleClass < String
20
+ # def public_hello
21
+ # "public hello"
22
+ # end
23
+ #
24
+ # protected
25
+ #
26
+ # def protected_hello
27
+ # "protected hello"
28
+ # end
29
+ #
30
+ # private
31
+ #
32
+ # def private_hello
33
+ # "private hello"
34
+ # end
35
+ # end
36
+ #
37
+ # SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
38
+ def my_methods
39
+ public_methods(false) + protected_methods(false) + private_methods(false)
40
+ end
41
+
42
+ # If self match any one of args, return true.
43
+ #
44
+ # "hoge".any_of? %w{hoge hige} # => true
45
+ # "hige".any_of? %w{hoge hige} # => true
46
+ # "hege".any_of? %w{hoge hige} # => false
47
+ def any_of?(*args)
48
+ args.each { |value|return true if self == value }
49
+ false
50
+ end
51
+
52
+ # you get bool value
53
+ #
54
+ # true.to_bool # => true
55
+ # false.to_bool # => false
56
+ # 0.to_bool # => true
57
+ # 1.to_bool # => true
58
+ # ''.to_bool # => true
59
+ # 'true'.to_bool # => true
60
+ # 'false'.to_bool # => true
61
+ # nil.to_bool # => false
62
+ def to_bool
63
+ !!self
64
+ end
65
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.16'
5
+ VERSION = '0.0.17'
6
6
  end
@@ -243,4 +243,99 @@ describe Array do
243
243
  end
244
244
  end
245
245
  end
246
+
247
+ context :together_reduce do
248
+ cases = [
249
+ {
250
+ case_no: 1,
251
+ case_title: 'single valid case',
252
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
253
+ init: nil,
254
+ logic: 'memo + first + second',
255
+ method_name: :together_reduce,
256
+ expected: 20,
257
+ },
258
+ {
259
+ case_no: 2,
260
+ case_title: 'single with init valid case',
261
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
262
+ init: 10,
263
+ logic: 'memo + first + second',
264
+ method_name: :together_reduce,
265
+ expected: 30,
266
+ },
267
+ {
268
+ case_no: 5,
269
+ case_title: 'single valid case',
270
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
271
+ logic: 'memo + first + second',
272
+ expected: 20,
273
+ method_name: :treduce,
274
+ },
275
+ {
276
+ case_no: 6,
277
+ case_title: 'single valid case',
278
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
279
+ logic: 'memo + first + second',
280
+ expected: 20,
281
+ method_name: :together_inject,
282
+ },
283
+ {
284
+ case_no: 7,
285
+ case_title: 'multi valid case',
286
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
287
+ logic: 'memo + first + second',
288
+ expected: 20,
289
+ method_name: :tinject,
290
+ },
291
+ {
292
+ case_no: 8,
293
+ case_title: 'single with init valid array case',
294
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
295
+ init: [],
296
+ logic: 'memo << first + second',
297
+ method_name: :together_reduce,
298
+ expected: [5, 4, 6, 5],
299
+ },
300
+ {
301
+ case_no: 9,
302
+ case_title: 'single with init valid array case',
303
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
304
+ init: {},
305
+ logic: 'memo[first] = second; memo',
306
+ method_name: :together_reduce,
307
+ expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
308
+ },
309
+ ]
310
+
311
+ cases.each do |c|
312
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
313
+ begin
314
+ case_before c
315
+
316
+ # -- given --
317
+ # nothing
318
+
319
+ # -- when/then --
320
+ if c[:init]
321
+ actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
322
+ else
323
+ actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
324
+ end
325
+
326
+ expect(actual).to eq(c[:expected])
327
+ ensure
328
+ case_after c
329
+ end
330
+ end
331
+
332
+ def case_before(c)
333
+ # implement each case before
334
+ end
335
+
336
+ def case_after(c)
337
+ # implement each case after
338
+ end
339
+ end
340
+ end
246
341
  end
data/tbpgr_utils.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec", "~> 2.14.1"
26
+ spec.add_development_dependency "simplecov", "~> 0.8.2"
26
27
  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.16
4
+ version: 0.0.17
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-01-23 00:00:00.000000000 Z
12
+ date: 2014-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &21415440 !ruby/object:Gem::Requirement
16
+ requirement: &20700768 !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: *21415440
24
+ version_requirements: *20700768
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &20573484 !ruby/object:Gem::Requirement
27
+ requirement: &20694180 !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: *20573484
35
+ version_requirements: *20694180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &21917292 !ruby/object:Gem::Requirement
38
+ requirement: &20712660 !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: *21917292
46
+ version_requirements: *20712660
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &20384916 !ruby/object:Gem::Requirement
49
+ requirement: &20734548 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,18 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *20384916
57
+ version_requirements: *20734548
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &20744088 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.8.2
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *20744088
58
69
  description: Utilities
59
70
  email:
60
71
  - tbpgr@tbpgr.jp