rtype 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
- require "rspec/core/rake_task"
2
-
3
- # Default pattern is 'spec/**{,/*/**}/*_spec.rb'
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
7
-
8
- # Benchmark
9
- desc "Compare with pure ruby and other gems"
10
- task :benchmark do
11
- ruby "benchmark/benchmark.rb"
1
+ require "rspec/core/rake_task"
2
+
3
+ # Default pattern is 'spec/**{,/*/**}/*_spec.rb'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ # Benchmark
9
+ desc "Compare with pure ruby and other gems"
10
+ task :benchmark do
11
+ ruby "benchmark/benchmark.rb"
12
12
  end
@@ -1,192 +1,192 @@
1
- require 'benchmark/ips'
2
-
3
- is_mri = RUBY_ENGINE == 'ruby'
4
-
5
- require "rtype"
6
- require "rubype" if is_mri
7
- require "sig"
8
- require "contracts"
9
- require "contracts/version"
10
- require "typecheck"
11
-
12
- puts "Ruby version: #{RUBY_VERSION}"
13
- puts "Ruby engine: #{RUBY_ENGINE}"
14
- puts "Ruby description: #{RUBY_DESCRIPTION}"
15
-
16
- puts "Rtype version: #{Rtype::VERSION}"
17
- puts "Rubype version: #{Rubype::VERSION}" if is_mri
18
- puts "Sig version: #{Sig::VERSION}"
19
- puts "Contracts version: #{Contracts::VERSION}"
20
- puts "Typecheck version: #{Typecheck::VERSION}"
21
-
22
- class PureTest
23
- def sum(x, y)
24
- x + y
25
- end
26
-
27
- def mul(x, y)
28
- x * y
29
- end
30
-
31
- def args(a, b, c, d)
32
- end
33
- end
34
- pure_obj = PureTest.new
35
-
36
- class RtypeTest
37
- rtype [Numeric, Numeric] => Numeric
38
- def sum(x, y)
39
- x + y
40
- end
41
-
42
- rtype [:to_i, :to_i] => Numeric
43
- def mul(x, y)
44
- x * y
45
- end
46
-
47
- rtype [Integer, Numeric, String, :to_i] => Any
48
- def args(a, b, c, d)
49
- end
50
- end
51
- rtype_obj = RtypeTest.new
52
-
53
- if is_mri
54
- class RubypeTest
55
- def sum(x, y)
56
- x + y
57
- end
58
- typesig :sum, [Numeric, Numeric] => Numeric
59
-
60
- def mul(x, y)
61
- x * y
62
- end
63
- typesig :mul, [:to_i, :to_i] => Numeric
64
-
65
- def args(a, b, c, d)
66
- end
67
- typesig :args, [Integer, Numeric, String, :to_i] => Any
68
- end
69
- rubype_obj = RubypeTest.new
70
- end
71
-
72
- class SigTest
73
- sig [Numeric, Numeric], Numeric,
74
- def sum(x, y)
75
- x + y
76
- end
77
-
78
- sig [:to_i, :to_i], Numeric,
79
- def mul(x, y)
80
- x * y
81
- end
82
-
83
- # nil means wildcard
84
- sig [Integer, Numeric, String, :to_i], nil,
85
- def args(a, b, c, d)
86
- end
87
- end
88
- sig_obj = SigTest.new
89
-
90
- class ContractsTest
91
- include Contracts
92
-
93
- Contract Num, Num => Num
94
- def sum(x, y)
95
- x + y
96
- end
97
-
98
- Contract RespondTo[:to_i], RespondTo[:to_i] => Num
99
- def mul(x, y)
100
- x * y
101
- end
102
-
103
- Contract Int, Num, String, RespondTo[:to_i] => Any
104
- def args(a, b, c, d)
105
- end
106
- end
107
- contracts_obj = ContractsTest.new
108
-
109
- class TypecheckTest
110
- extend Typecheck
111
-
112
- typecheck 'Numeric, Numeric -> Numeric',
113
- def sum(x, y)
114
- x + y
115
- end
116
-
117
- typecheck '#to_i, #to_i -> Numeric',
118
- def mul(x, y)
119
- x * y
120
- end
121
-
122
- typecheck 'Integer, Numeric, String, #to_i -> BasicObject',
123
- def args(a, b, c, d)
124
- end
125
- end
126
- typecheck_obj = TypecheckTest.new
127
-
128
- Benchmark.ips do |x|
129
- x.report("pure") do |times|
130
- i = 0
131
- while i < times
132
- pure_obj.sum(1, 2)
133
- pure_obj.mul(1, 2)
134
- pure_obj.args(1, 2, "c", 4)
135
- i += 1
136
- end
137
- end
138
-
139
- x.report("rtype") do |times|
140
- i = 0
141
- while i < times
142
- rtype_obj.sum(1, 2)
143
- rtype_obj.mul(1, 2)
144
- rtype_obj.args(1, 2, "c", 4)
145
- i += 1
146
- end
147
- end
148
-
149
- if is_mri
150
- x.report("rubype") do |times|
151
- i = 0
152
- while i < times
153
- rubype_obj.sum(1, 2)
154
- rubype_obj.mul(1, 2)
155
- rubype_obj.args(1, 2, "c", 4)
156
- i += 1
157
- end
158
- end
159
- end
160
-
161
- x.report("sig") do |times|
162
- i = 0
163
- while i < times
164
- sig_obj.sum(1, 2)
165
- sig_obj.mul(1, 2)
166
- sig_obj.args(1, 2, "c", 4)
167
- i += 1
168
- end
169
- end
170
-
171
- x.report("contracts") do |times|
172
- i = 0
173
- while i < times
174
- contracts_obj.sum(1, 2)
175
- contracts_obj.mul(1, 2)
176
- contracts_obj.args(1, 2, "c", 4)
177
- i += 1
178
- end
179
- end
180
-
181
- x.report("typecheck") do |times|
182
- i = 0
183
- while i < times
184
- typecheck_obj.sum(1, 2)
185
- typecheck_obj.mul(1, 2)
186
- typecheck_obj.args(1, 2, "c", 4)
187
- i += 1
188
- end
189
- end
190
-
191
- x.compare!
1
+ require 'benchmark/ips'
2
+
3
+ is_mri = RUBY_ENGINE == 'ruby'
4
+
5
+ require "rtype"
6
+ require "rubype" if is_mri
7
+ require "sig"
8
+ require "contracts"
9
+ require "contracts/version"
10
+ require "typecheck"
11
+
12
+ puts "Ruby version: #{RUBY_VERSION}"
13
+ puts "Ruby engine: #{RUBY_ENGINE}"
14
+ puts "Ruby description: #{RUBY_DESCRIPTION}"
15
+
16
+ puts "Rtype version: #{Rtype::VERSION}"
17
+ puts "Rubype version: #{Rubype::VERSION}" if is_mri
18
+ puts "Sig version: #{Sig::VERSION}"
19
+ puts "Contracts version: #{Contracts::VERSION}"
20
+ puts "Typecheck version: #{Typecheck::VERSION}"
21
+
22
+ class PureTest
23
+ def sum(x, y)
24
+ x + y
25
+ end
26
+
27
+ def mul(x, y)
28
+ x * y
29
+ end
30
+
31
+ def args(a, b, c, d)
32
+ end
33
+ end
34
+ pure_obj = PureTest.new
35
+
36
+ class RtypeTest
37
+ rtype [Numeric, Numeric] => Numeric
38
+ def sum(x, y)
39
+ x + y
40
+ end
41
+
42
+ rtype [:to_i, :to_i] => Numeric
43
+ def mul(x, y)
44
+ x * y
45
+ end
46
+
47
+ rtype [Integer, Numeric, String, :to_i] => Any
48
+ def args(a, b, c, d)
49
+ end
50
+ end
51
+ rtype_obj = RtypeTest.new
52
+
53
+ if is_mri
54
+ class RubypeTest
55
+ def sum(x, y)
56
+ x + y
57
+ end
58
+ typesig :sum, [Numeric, Numeric] => Numeric
59
+
60
+ def mul(x, y)
61
+ x * y
62
+ end
63
+ typesig :mul, [:to_i, :to_i] => Numeric
64
+
65
+ def args(a, b, c, d)
66
+ end
67
+ typesig :args, [Integer, Numeric, String, :to_i] => Any
68
+ end
69
+ rubype_obj = RubypeTest.new
70
+ end
71
+
72
+ class SigTest
73
+ sig [Numeric, Numeric], Numeric,
74
+ def sum(x, y)
75
+ x + y
76
+ end
77
+
78
+ sig [:to_i, :to_i], Numeric,
79
+ def mul(x, y)
80
+ x * y
81
+ end
82
+
83
+ # nil means wildcard
84
+ sig [Integer, Numeric, String, :to_i], nil,
85
+ def args(a, b, c, d)
86
+ end
87
+ end
88
+ sig_obj = SigTest.new
89
+
90
+ class ContractsTest
91
+ include Contracts
92
+
93
+ Contract Num, Num => Num
94
+ def sum(x, y)
95
+ x + y
96
+ end
97
+
98
+ Contract RespondTo[:to_i], RespondTo[:to_i] => Num
99
+ def mul(x, y)
100
+ x * y
101
+ end
102
+
103
+ Contract Int, Num, String, RespondTo[:to_i] => Any
104
+ def args(a, b, c, d)
105
+ end
106
+ end
107
+ contracts_obj = ContractsTest.new
108
+
109
+ class TypecheckTest
110
+ extend Typecheck
111
+
112
+ typecheck 'Numeric, Numeric -> Numeric',
113
+ def sum(x, y)
114
+ x + y
115
+ end
116
+
117
+ typecheck '#to_i, #to_i -> Numeric',
118
+ def mul(x, y)
119
+ x * y
120
+ end
121
+
122
+ typecheck 'Integer, Numeric, String, #to_i -> BasicObject',
123
+ def args(a, b, c, d)
124
+ end
125
+ end
126
+ typecheck_obj = TypecheckTest.new
127
+
128
+ Benchmark.ips do |x|
129
+ x.report("pure") do |times|
130
+ i = 0
131
+ while i < times
132
+ pure_obj.sum(1, 2)
133
+ pure_obj.mul(1, 2)
134
+ pure_obj.args(1, 2, "c", 4)
135
+ i += 1
136
+ end
137
+ end
138
+
139
+ x.report("rtype") do |times|
140
+ i = 0
141
+ while i < times
142
+ rtype_obj.sum(1, 2)
143
+ rtype_obj.mul(1, 2)
144
+ rtype_obj.args(1, 2, "c", 4)
145
+ i += 1
146
+ end
147
+ end
148
+
149
+ if is_mri
150
+ x.report("rubype") do |times|
151
+ i = 0
152
+ while i < times
153
+ rubype_obj.sum(1, 2)
154
+ rubype_obj.mul(1, 2)
155
+ rubype_obj.args(1, 2, "c", 4)
156
+ i += 1
157
+ end
158
+ end
159
+ end
160
+
161
+ x.report("sig") do |times|
162
+ i = 0
163
+ while i < times
164
+ sig_obj.sum(1, 2)
165
+ sig_obj.mul(1, 2)
166
+ sig_obj.args(1, 2, "c", 4)
167
+ i += 1
168
+ end
169
+ end
170
+
171
+ x.report("contracts") do |times|
172
+ i = 0
173
+ while i < times
174
+ contracts_obj.sum(1, 2)
175
+ contracts_obj.mul(1, 2)
176
+ contracts_obj.args(1, 2, "c", 4)
177
+ i += 1
178
+ end
179
+ end
180
+
181
+ x.report("typecheck") do |times|
182
+ i = 0
183
+ while i < times
184
+ typecheck_obj.sum(1, 2)
185
+ typecheck_obj.mul(1, 2)
186
+ typecheck_obj.args(1, 2, "c", 4)
187
+ i += 1
188
+ end
189
+ end
190
+
191
+ x.compare!
192
192
  end
@@ -1,4 +1,4 @@
1
- module Rtype
2
- class ArgumentTypeError < ArgumentError
3
- end
1
+ module Rtype
2
+ class ArgumentTypeError < ArgumentError
3
+ end
4
4
  end
@@ -1,24 +1,24 @@
1
- module Rtype
2
- module Behavior
3
- class And < Base
4
- def initialize(*types)
5
- @types = types
6
- end
7
-
8
- def valid?(value)
9
- @types.all? do |e|
10
- Rtype::valid? e, value
11
- end
12
- end
13
-
14
- def error_message(value)
15
- arr = @types.map { |e| Rtype::type_error_message(e, value) }
16
- arr.join "\nAND "
17
- end
18
- end
19
- end
20
-
21
- def self.and(*args)
22
- Behavior::And[*args]
23
- end
1
+ module Rtype
2
+ module Behavior
3
+ class And < Base
4
+ def initialize(*types)
5
+ @types = types
6
+ end
7
+
8
+ def valid?(value)
9
+ @types.all? do |e|
10
+ Rtype::valid? e, value
11
+ end
12
+ end
13
+
14
+ def error_message(value)
15
+ arr = @types.map { |e| Rtype::type_error_message(e, value) }
16
+ arr.join "\nAND "
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.and(*args)
22
+ Behavior::And[*args]
23
+ end
24
24
  end
@@ -1,17 +1,17 @@
1
- module Rtype
2
- module Behavior
3
- class Base
4
- def self.[](*vals)
5
- new(*vals)
6
- end
7
-
8
- def valid?(value)
9
- raise NotImplementedError, "Abstract method"
10
- end
11
-
12
- def error_message(value)
13
- raise NotImplementedError, "Abstract method"
14
- end
15
- end
16
- end
1
+ module Rtype
2
+ module Behavior
3
+ class Base
4
+ def self.[](*vals)
5
+ new(*vals)
6
+ end
7
+
8
+ def valid?(value)
9
+ raise NotImplementedError, "Abstract method"
10
+ end
11
+
12
+ def error_message(value)
13
+ raise NotImplementedError, "Abstract method"
14
+ end
15
+ end
16
+ end
17
17
  end
@@ -1,22 +1,22 @@
1
- class Object
2
- def and(*others)
3
- ::Rtype::and(self, *others)
4
- end
5
-
6
- def nilable
7
- ::Rtype::nilable(self)
8
- end
9
- alias_method :or_nil, :nilable
10
-
11
- def not
12
- ::Rtype::not(self)
13
- end
14
-
15
- def or(*others)
16
- ::Rtype::or(self, *others)
17
- end
18
-
19
- def xor(*others)
20
- ::Rtype::xor(self, *others)
21
- end
1
+ class Object
2
+ def and(*others)
3
+ ::Rtype::and(self, *others)
4
+ end
5
+
6
+ def nilable
7
+ ::Rtype::nilable(self)
8
+ end
9
+ alias_method :or_nil, :nilable
10
+
11
+ def not
12
+ ::Rtype::not(self)
13
+ end
14
+
15
+ def or(*others)
16
+ ::Rtype::or(self, *others)
17
+ end
18
+
19
+ def xor(*others)
20
+ ::Rtype::xor(self, *others)
21
+ end
22
22
  end
@@ -1,21 +1,21 @@
1
- module Rtype
2
- module Behavior
3
- class Nilable < Base
4
- def initialize(type)
5
- @type = type
6
- end
7
-
8
- def valid?(value)
9
- value.nil? || Rtype::valid?(@type, value)
10
- end
11
-
12
- def error_message(value)
13
- Rtype::type_error_message(@type, value) + "\nOR " + Rtype::type_error_message(nil, value)
14
- end
15
- end
16
- end
17
-
18
- def nilable(*args)
19
- Behavior::Nilable[*args]
20
- end
1
+ module Rtype
2
+ module Behavior
3
+ class Nilable < Base
4
+ def initialize(type)
5
+ @type = type
6
+ end
7
+
8
+ def valid?(value)
9
+ value.nil? || Rtype::valid?(@type, value)
10
+ end
11
+
12
+ def error_message(value)
13
+ Rtype::type_error_message(@type, value) + "\nOR " + Rtype::type_error_message(nil, value)
14
+ end
15
+ end
16
+ end
17
+
18
+ def nilable(*args)
19
+ Behavior::Nilable[*args]
20
+ end
21
21
  end
@@ -1,24 +1,24 @@
1
- module Rtype
2
- module Behavior
3
- class Not < Base
4
- def initialize(*types)
5
- @types = types
6
- end
7
-
8
- def valid?(value)
9
- @types.all? do |e|
10
- !Rtype::valid?(e, value)
11
- end
12
- end
13
-
14
- def error_message(value)
15
- arr = @types.map { |e| "NOT " + Rtype::type_error_message(e, value) }
16
- arr.join "\nAND "
17
- end
18
- end
19
- end
20
-
21
- def not(*args)
22
- Behavior::Not[*args]
23
- end
1
+ module Rtype
2
+ module Behavior
3
+ class Not < Base
4
+ def initialize(*types)
5
+ @types = types
6
+ end
7
+
8
+ def valid?(value)
9
+ @types.all? do |e|
10
+ !Rtype::valid?(e, value)
11
+ end
12
+ end
13
+
14
+ def error_message(value)
15
+ arr = @types.map { |e| "NOT " + Rtype::type_error_message(e, value) }
16
+ arr.join "\nAND "
17
+ end
18
+ end
19
+ end
20
+
21
+ def not(*args)
22
+ Behavior::Not[*args]
23
+ end
24
24
  end
@@ -1,24 +1,24 @@
1
- module Rtype
2
- module Behavior
3
- class Or < Base
4
- def initialize(*types)
5
- @types = types
6
- end
7
-
8
- def valid?(value)
9
- @types.any? do |e|
10
- Rtype::valid? e, value
11
- end
12
- end
13
-
14
- def error_message(value)
15
- arr = @types.map { |e| Rtype::type_error_message(e, value) }
16
- arr.join "\nOR "
17
- end
18
- end
19
- end
20
-
21
- def or(*args)
22
- Behavior::Or[*args]
23
- end
1
+ module Rtype
2
+ module Behavior
3
+ class Or < Base
4
+ def initialize(*types)
5
+ @types = types
6
+ end
7
+
8
+ def valid?(value)
9
+ @types.any? do |e|
10
+ Rtype::valid? e, value
11
+ end
12
+ end
13
+
14
+ def error_message(value)
15
+ arr = @types.map { |e| Rtype::type_error_message(e, value) }
16
+ arr.join "\nOR "
17
+ end
18
+ end
19
+ end
20
+
21
+ def or(*args)
22
+ Behavior::Or[*args]
23
+ end
24
24
  end