typ 0.0.5 → 0.0.6

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/typ.rb +107 -37
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14188b8d650c32fecbae6ed1ae3c8f03eac9f31effad6a0695c8ec1e77f8bdbe
4
- data.tar.gz: 89524fac30d45f13f8a21f904c22e9890fb3a8d2f59bb61ad53ef5ca6abfb7d4
3
+ metadata.gz: a726dc4e0d96b4f5bdad9858d10063bf818af3e5bd1293135acffa040bbde236
4
+ data.tar.gz: fbc8d499fb78954604a757593e2e46a9e2af7f5fba4102783d6011aa991a10f3
5
5
  SHA512:
6
- metadata.gz: a08274c0bed76f596da8d0b30b45bfea0679c2ba8ba29b05e478f6ce2fc3366d3110e612a8f0c2b72ac9a0262aeb7391da8a30c4915c62464eeb25b8373b4bd1
7
- data.tar.gz: defa65890558d113926b445b3e5a6161d80f645bd98d0ba5e3d2ff2f602ca62d64ac885378d140dc1d12d3deaec55897a497b28c567226afd506684ef0d2b585
6
+ metadata.gz: 2d3679f431008960d32befdaea22720e3a3cf49692c7a81f19463476b8cae6d67c8069d3920c1325a3c68d743058a348f2087295219f75bde0261bb780b95cdd
7
+ data.tar.gz: 20af3243c88900e428928f2c770cb661ee69835187fb0834f55d7be701ab3cfbf0f848b2a854f8998e42cd85291c4551da5b6eaf7ceb7946d63f71aadc823bfb
data/lib/typ.rb CHANGED
@@ -1,18 +1,35 @@
1
1
  module Typ
2
+ module Gate
3
+ attr_reader :it
4
+
5
+ def initialize it
6
+ @it = it
7
+ check
8
+ end
9
+
10
+ # Should set @ok to be true or false
11
+ def check
12
+ @ok = self.class.check === it
13
+ end
14
+
15
+ def ok?
16
+ @ok
17
+ end
18
+ end
19
+
2
20
  def self.included mod
3
21
  mod.extend DSL
22
+ mod.extend PatternMatching
4
23
  end
5
24
 
6
- def initialize it
7
- @it = it
8
- @gates = self.class.gates.map { |gate| gate.new it }
9
- @fails = gates.reject &:ok?
10
- end
25
+ include Gate
11
26
 
12
- attr_reader :it, :gates, :fails
27
+ attr_reader :gates, :fails
13
28
 
14
- def ok?
15
- fails.empty?
29
+ def check
30
+ @gates = self.class.gates.map { |gate| gate.new it }
31
+ @fails = gates.reject &:ok?
32
+ @ok = fails.empty?
16
33
  end
17
34
 
18
35
  module DSL
@@ -35,10 +52,10 @@ module Typ
35
52
  end
36
53
  end
37
54
 
38
- def is_a type
55
+ def is_a type, params = {}
39
56
  case type
40
57
  when Module
41
- gates << IsA.new(type)
58
+ gates << IsA.new(type, params)
42
59
  else
43
60
  fail "don't know how to create a Gate from #{type}"
44
61
  end
@@ -48,21 +65,36 @@ module Typ
48
65
  module Is
49
66
 
50
67
  module Array
51
- class << self
52
- def new array
53
- check = if array[0].is_a?(Symbol)
68
+ class Check
69
+ def initialize array
70
+ @array = array
71
+ @check = if array[0].is_a?(Symbol)
54
72
  method, *arguments = array
55
- -> receiver { receiver.send method, *arguments }
73
+ -> it { it.send method, *arguments }
56
74
  elsif array[1].is_a?(Symbol)
57
75
  receiver, method = array
58
- -> argument { receiver.send method, argument }
76
+ -> it { receiver.send method, it }
59
77
  else
60
78
  fail "not sure how to handle #{array} yet"
61
79
  end
80
+ end
81
+
82
+ def === it
83
+ @check === it
84
+ end
85
+
86
+ def to_a
87
+ @array
88
+ end
89
+ end
90
+
91
+ class << self
92
+ def new array
93
+ check = Check.new array
62
94
 
63
95
  gate = Class.new
64
96
  gate.include self
65
- gate.check, gate.array = check, array
97
+ gate.check = check
66
98
  gate
67
99
  end
68
100
  end
@@ -72,31 +104,25 @@ module Typ
72
104
  end
73
105
 
74
106
  module Singleton
75
- attr_accessor :check, :array
107
+ attr_accessor :check
76
108
  end
77
109
 
78
110
  def to_a
79
- self.class.array
80
- end
81
-
82
- def ok?
83
- @ok
111
+ self.class.check.to_a
84
112
  end
85
113
 
86
- def initialize it
87
- @ok = self.class.check[it]
88
- end
114
+ include Gate
89
115
  end
90
116
  end
91
117
 
92
118
  module IsA
93
119
  class << self
94
- def new type
95
- check = -> it { it.is_a? type }
120
+ def new type, params = {}
121
+ type = Type.new type, params
96
122
 
97
123
  gate = Class.new
98
124
  gate.include self
99
- gate.type, gate.check = type, check
125
+ gate.check = type
100
126
  gate
101
127
  end
102
128
  end
@@ -106,18 +132,62 @@ module Typ
106
132
  end
107
133
 
108
134
  module Singleton
109
- attr_accessor :type, :check
135
+ attr_accessor :check
110
136
  end
111
137
 
112
- def ok?
113
- @ok
138
+ include Gate
139
+
140
+ class Type
141
+ def initialize type, params = {}
142
+ @type, @params = type, params
143
+
144
+ @param = params[:of]
145
+
146
+ @check = if @params.empty?
147
+ -> it { it.is_a? @type }
148
+ else
149
+ if @param
150
+ check_Array
151
+ else
152
+ check_Hash
153
+ end
154
+ end
155
+ end
156
+
157
+ def === it
158
+ @check === it
159
+ end
160
+
161
+ def to_s
162
+ if @params.empty?
163
+ @type.to_s
164
+ else
165
+ "#{@type}, #{@params}"
166
+ end
167
+ end
168
+
169
+ private
170
+ def check_Array
171
+ -> it {
172
+ it.is_a?(@type) &&
173
+ it.all? { |element| element.is_a? @param }
174
+ }
175
+ end
176
+
177
+ def check_Hash
178
+ key_type, value_type = @params.first
179
+ -> it {
180
+ it.is_a?(@type) &&
181
+ it.keys.all? {|k| k.is_a? key_type } &&
182
+ it.values.all? {|v| v.is_a? value_type }
183
+ }
184
+ end
114
185
  end
115
-
116
- def initialize it
117
- @it = it
118
- @ok = self.class.check[it]
186
+ end
187
+
188
+ module PatternMatching
189
+ def === it
190
+ new(it).ok?
119
191
  end
120
-
121
- attr_reader :it
122
192
  end
123
193
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typ
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: