patternmatching 0.1.2 → 0.1.3
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/History.txt +5 -0
- data/Manifest.txt +1 -0
- data/README.txt +142 -139
- data/Rakefile +2 -1
- data/examples/match_inside_class.rb +28 -0
- data/lib/patternmatching/pattern.rb +79 -132
- data/lib/patternmatching/version.rb +1 -1
- data/lib/patternmatching.rb +108 -2
- data/spec/patternmatching_spec.rb +29 -0
- data/website/index.html +20 -5
- data/website/index.txt +12 -3
- metadata +3 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1,171 +1,174 @@
|
|
1
|
-
|
1
|
+
= PatternMatching module
|
2
2
|
|
3
|
-
h1. → 'patternmatching'
|
4
3
|
|
5
|
-
|
4
|
+
== What
|
6
5
|
|
7
6
|
Provides a pure ruby module that:
|
8
7
|
* can build structured objects easily
|
9
8
|
* can enable pattern match of objects
|
10
9
|
* can define method as a partial function style
|
11
10
|
|
12
|
-
|
11
|
+
== Installing
|
13
12
|
|
14
|
-
|
13
|
+
sudo gem install patternmatching
|
15
14
|
|
16
|
-
|
15
|
+
== The basics
|
17
16
|
|
18
17
|
This module provides methods for tree pattern matching features.
|
19
18
|
|
20
|
-
* For detail, see Wikipedia:
|
19
|
+
* For detail, see {Wikipedia: Pattern matching}[http://en.wikipedia.org/wiki/Pattern_matching]
|
21
20
|
* Syntax based on meta-programming, like "rspec", and so on.
|
22
21
|
|
23
22
|
Note: Default equivalence used in structured pattern matching is
|
24
|
-
based on "
|
25
|
-
so "
|
23
|
+
based on "pattern === data",
|
24
|
+
so "foo(Numeric)" matches "foo(100)".
|
26
25
|
|
27
26
|
Notice: Current implementation is not thread safe now.
|
28
27
|
Need the receiver object(NOT an argument) calling pattern matching
|
29
28
|
synchronized when multi-threaded access.
|
30
29
|
|
31
|
-
|
30
|
+
== Demonstration of usage
|
32
31
|
|
33
|
-
Symbols(e.g.
|
32
|
+
Symbols(e.g. ":a", ":right_value")
|
34
33
|
in patterns is passed as variable
|
35
34
|
to the following block when the pattern matched.
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
require "
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
require "patternmatching"
|
88
|
-
|
89
|
-
include PatternMatching
|
90
|
-
|
91
|
-
# Example for matching Enumerable
|
92
|
-
is = build { exact([1,2,3,4,5]) }
|
93
|
-
make is do
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end # => "1, 2, and 345"
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
h2. License
|
36
|
+
=== Pattern matching expression
|
37
|
+
# If installed from gem
|
38
|
+
# require "rubygems"
|
39
|
+
# gem "patternmatching"
|
40
|
+
require "patternmatching"
|
41
|
+
|
42
|
+
# For DSL style code, include PatternMatching
|
43
|
+
include PatternMatching
|
44
|
+
|
45
|
+
# match example
|
46
|
+
def calc(code)
|
47
|
+
make(code) {
|
48
|
+
seems as {plus(:a, :b)} do calc(a) + calc(b) end
|
49
|
+
seems as {mul(:a, :b)} do calc(a) * calc(b) end
|
50
|
+
seems something do code end
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
code = build {plus(mul(100, 100), 200)}
|
55
|
+
p calc(code) #=> 10200
|
56
|
+
|
57
|
+
=== Partial style method
|
58
|
+
|
59
|
+
require "patternmatching"
|
60
|
+
|
61
|
+
# Structured data builder
|
62
|
+
code = PatternMatching.build {plus(mul(100, 100), 200)}
|
63
|
+
|
64
|
+
# Partial style method example
|
65
|
+
class Calc
|
66
|
+
# At first, extends with the module
|
67
|
+
extend PatternMatching
|
68
|
+
|
69
|
+
# def calcm(o), as 3 partial styles
|
70
|
+
func(:calcm).seems as {plus(:a, :b)} do
|
71
|
+
calcm(b) + calcm(b)
|
72
|
+
end
|
73
|
+
func(:calcm).seems as {mul(:a, :b)} do
|
74
|
+
calcm(a) * calcm(b)
|
75
|
+
end
|
76
|
+
func(:calcm).seems as {:value} do
|
77
|
+
value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# use as standard method
|
82
|
+
p Calc.new.calcm(code) #=> 10200
|
83
|
+
|
84
|
+
=== Array/Enumerable pattern
|
85
|
+
|
86
|
+
require "patternmatching"
|
87
|
+
|
88
|
+
include PatternMatching
|
89
|
+
|
90
|
+
# Example for matching Enumerable
|
91
|
+
is = build { exact([1,2,3,4,5]) }
|
92
|
+
make is do
|
93
|
+
# _! matches rest of lists
|
94
|
+
seems as {exact([:a,:b, _!(:c)])} do
|
95
|
+
puts a.to_s + ", " + b.to_s + " and " + c.to_s
|
96
|
+
end
|
97
|
+
seems something do
|
98
|
+
puts "not matched"
|
99
|
+
end
|
100
|
+
end # => "1, 2, and 345"
|
101
|
+
|
102
|
+
=== Hash pattern
|
103
|
+
|
104
|
+
require "patternmatching"
|
105
|
+
|
106
|
+
include PatternMatching
|
107
|
+
|
108
|
+
# Example for matching Hash
|
109
|
+
dict = build { {:name => "Taro", :age => 5} }
|
110
|
+
make dict do
|
111
|
+
seems as {{:name => :name}} do
|
112
|
+
puts "He is " + name
|
113
|
+
end
|
114
|
+
seems something do
|
115
|
+
puts "no name"
|
116
|
+
end
|
117
|
+
end # => "He is Taro"
|
118
|
+
|
119
|
+
=== Non-Hash/Object pattern
|
120
|
+
|
121
|
+
require "patternmatching"
|
122
|
+
|
123
|
+
include PatternMatching
|
124
|
+
|
125
|
+
class Person
|
126
|
+
def initialize(name, age)
|
127
|
+
@name = name
|
128
|
+
@age = age
|
129
|
+
end
|
130
|
+
attr :name
|
131
|
+
attr :age
|
132
|
+
end
|
133
|
+
|
134
|
+
# Example for matching Object except Hash
|
135
|
+
person = Person.new("Jiro", 3)
|
136
|
+
make person do
|
137
|
+
seems as {{:name => :name}} do
|
138
|
+
puts "He is " + name
|
139
|
+
end
|
140
|
+
seems something do
|
141
|
+
puts "no name"
|
142
|
+
end
|
143
|
+
end # => "He is Jiro"
|
144
|
+
|
145
|
+
== Forum
|
146
|
+
|
147
|
+
Visit the project forum in RubyForge.
|
148
|
+
|
149
|
+
== How to submit patches
|
150
|
+
|
151
|
+
Read the {8 steps for fixing other people's code}[http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/] and for section {8b: Submit patch to Google Groups}[http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups], use the forum above.
|
152
|
+
|
153
|
+
The trunk repository is
|
154
|
+
svn://rubyforge.org/var/svn/patternmatching/trunk
|
155
|
+
for anonymous access.
|
156
|
+
|
157
|
+
== License
|
160
158
|
|
161
159
|
This code is free to use under the terms of the MIT license.
|
162
160
|
|
163
|
-
|
161
|
+
== Link
|
164
162
|
|
165
|
-
*
|
166
|
-
*
|
163
|
+
* Web Site: http://patternmatching.rubyforge.org/
|
164
|
+
* Project Page: http://rubyforge.org/projects/patternmatching/
|
167
165
|
|
168
|
-
|
166
|
+
== Contact
|
169
167
|
|
170
|
-
Comments are welcome. Send
|
168
|
+
Comments are welcome. Send a message to the forum.
|
169
|
+
{My blog}[http://d.hatena.ne.jp/bellbind/] (written in Japanese)
|
170
|
+
could be help you.
|
171
171
|
|
172
|
+
== Authors
|
173
|
+
|
174
|
+
* ICHIYAMA Ryoichi: bellbind at gmail dot com
|
data/Rakefile
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "patternmatching"
|
2
|
+
|
3
|
+
include PatternMatching
|
4
|
+
|
5
|
+
class Foo
|
6
|
+
def initialize
|
7
|
+
@name = "Foo"
|
8
|
+
end
|
9
|
+
attr :name
|
10
|
+
|
11
|
+
def bar
|
12
|
+
make "bar" do
|
13
|
+
seems as {:val} do
|
14
|
+
@name = val
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
func(:buzz).seems as {:val} do
|
20
|
+
@name = val
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
o = Foo.new
|
25
|
+
o.bar
|
26
|
+
p o.name #=> "bar"
|
27
|
+
o.buzz("buzz")
|
28
|
+
p o.name #=> "buzz"
|
@@ -1,14 +1,18 @@
|
|
1
1
|
|
2
2
|
module PatternMatching
|
3
|
+
#Private Exception for stop matching
|
3
4
|
class NotMatched < Exception
|
4
5
|
end
|
5
6
|
|
7
|
+
#Private class for build structured data
|
6
8
|
class NodeBuilder
|
9
|
+
private
|
7
10
|
def method_missing(name, *args)
|
8
11
|
Node.new(name, args)
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
15
|
+
#Class for structured data/patterns
|
12
16
|
class Node
|
13
17
|
def initialize(name, children)
|
14
18
|
@name = name
|
@@ -23,7 +27,8 @@ module PatternMatching
|
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
26
|
-
|
30
|
+
#Private module for pattern matching and to collect var/val pairs
|
31
|
+
module Collector
|
27
32
|
def self.walk(source, target, list)
|
28
33
|
case source
|
29
34
|
when Symbol
|
@@ -99,6 +104,7 @@ module PatternMatching
|
|
99
104
|
end
|
100
105
|
end
|
101
106
|
|
107
|
+
#Private class to run pattern matching
|
102
108
|
class MatchExec
|
103
109
|
def self.exec_as(target, patterns, receiver)
|
104
110
|
patterns.each do |pair|
|
@@ -115,6 +121,8 @@ module PatternMatching
|
|
115
121
|
end
|
116
122
|
end
|
117
123
|
|
124
|
+
#Private class enabling to use the name of symbols in patterns
|
125
|
+
#like a local variables in action blocks
|
118
126
|
class ExecuteAs
|
119
127
|
def initialize(args, receiver)
|
120
128
|
@this = receiver
|
@@ -141,6 +149,7 @@ module PatternMatching
|
|
141
149
|
end
|
142
150
|
end
|
143
151
|
|
152
|
+
#Private class for collecting pattern/action fragments
|
144
153
|
class PatternFragments
|
145
154
|
def initialize(patterns)
|
146
155
|
@patterns = patterns
|
@@ -157,141 +166,79 @@ module PatternMatching
|
|
157
166
|
end
|
158
167
|
end
|
159
168
|
|
160
|
-
#
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
169
|
+
# Domain Specific Language style methods
|
170
|
+
module DSL
|
171
|
+
#Build structured data
|
172
|
+
#=== Usage
|
173
|
+
# build {[foo(bar, 100), foo(buzz, "abc")]}
|
174
|
+
def build(&block)
|
175
|
+
NodeBuilder.new.instance_eval(&block)
|
176
|
+
end
|
177
|
+
|
178
|
+
#Build structured data
|
179
|
+
#=== Usage
|
180
|
+
# PatternMatching.build {[foo(bar, 100), foo(buzz, "abc")]}
|
181
|
+
def self.build(&block)
|
182
|
+
NodeBuilder.new.instance_eval(&block)
|
183
|
+
end
|
184
|
+
|
185
|
+
#Do pattern matching
|
186
|
+
#===Usage
|
187
|
+
# make TARGET do
|
188
|
+
# seems as {PATTERN_1} do ACTION_1 end
|
189
|
+
# seems as {PATTERN_2} do ACTION_2 end
|
190
|
+
# seems something do ACTION_DEFAULT end
|
191
|
+
# end
|
192
|
+
def make(target, &block)
|
181
193
|
patterns = []
|
182
|
-
|
183
|
-
|
184
|
-
MatchExec.exec_as(target, patterns, self)
|
185
|
-
end
|
194
|
+
PatternFragments.new(patterns).instance_eval(&block)
|
195
|
+
MatchExec.exec_as(target, patterns, self)
|
186
196
|
end
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
197
|
+
|
198
|
+
#A pattern matches description inside block
|
199
|
+
#=== Usage
|
200
|
+
# seems as {some pattern...} do ... end
|
201
|
+
def as(&block)
|
202
|
+
block
|
191
203
|
end
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
require "rubygems"
|
199
|
-
gem "patternmatching"
|
200
|
-
|
201
|
-
# for use
|
202
|
-
require "patternmatching"
|
203
|
-
|
204
|
-
|
205
|
-
# partial func example
|
206
|
-
class Calc
|
207
|
-
extend PatternMatching
|
208
|
-
|
209
|
-
func(:calcm).seems as {plus(:a, :b)} do
|
210
|
-
calcm(a) + calcm(b)
|
211
|
-
end
|
212
|
-
func(:calcm).seems as {mul(:a, :b)} do
|
213
|
-
calcm(a) * calcm(b)
|
214
|
-
end
|
215
|
-
func(:calcm).seems as {:value} do
|
216
|
-
value
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
val = 200
|
221
|
-
code = PatternMatching.build {plus(mul(100, 100), val)}
|
222
|
-
p Calc.new.calcm(code)
|
223
|
-
|
224
|
-
# another partial func example
|
225
|
-
class CalcX
|
226
|
-
extend PatternMatching
|
227
|
-
|
228
|
-
func(:calcx) do
|
229
|
-
seems as {plus(:a, :b)} do
|
230
|
-
calcx(a) + calcx(b)
|
204
|
+
|
205
|
+
#A pattern matches anything
|
206
|
+
#=== Usage
|
207
|
+
# seems something do ... end
|
208
|
+
def something
|
209
|
+
proc {_}
|
231
210
|
end
|
232
|
-
|
233
|
-
|
211
|
+
|
212
|
+
#Define method as partial style
|
213
|
+
#=== Usage
|
214
|
+
# func(NAME).seems as {PATTERN_1} do ACTION_1 end
|
215
|
+
# func(NAME).seems as {PATTERN_2} do ACTION_2 end
|
216
|
+
# func(NAME).seems something do ACTION_DEFAULT end
|
217
|
+
#
|
218
|
+
#or
|
219
|
+
#
|
220
|
+
# func NAME do
|
221
|
+
# seems as {PATTERN_1} do ACTION_1 end
|
222
|
+
# seems as {PATTERN_2} do ACTION_2 end
|
223
|
+
# seems something do ACTION_DEFAULT end
|
224
|
+
# end
|
225
|
+
#
|
226
|
+
def func(name, &block)
|
227
|
+
pattern_name = ("@_pattern_" + name.to_s ).to_sym
|
228
|
+
unless method_defined?(name)
|
229
|
+
patterns = []
|
230
|
+
instance_variable_set(pattern_name, patterns)
|
231
|
+
define_method(name) do |target|
|
232
|
+
MatchExec.exec_as(target, patterns, self)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
patterns = instance_variable_get(pattern_name)
|
236
|
+
fragments = PatternFragments.new(patterns)
|
237
|
+
if block
|
238
|
+
fragments.instance_eval(&block)
|
239
|
+
end
|
240
|
+
fragments
|
234
241
|
end
|
235
242
|
end
|
236
|
-
|
237
|
-
value
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
p CalcX.new.calcx(code)
|
242
|
-
|
243
|
-
# pattern example
|
244
|
-
include PatternMatching
|
245
|
-
|
246
|
-
def calc(code)
|
247
|
-
make(code) {
|
248
|
-
seems as {plus(:a, :b)} do calc(a) + calc(b) end
|
249
|
-
seems as {mul(:a, :b)} do calc(a) * calc(b) end
|
250
|
-
seems something do code end
|
251
|
-
}
|
252
|
-
end
|
253
|
-
|
254
|
-
p calc(code)
|
255
|
-
|
256
|
-
|
257
|
-
# enumerable match example
|
258
|
-
is = build { exact([1,2,3,4,5]) }
|
259
|
-
make is do
|
260
|
-
seems as {exact([:a,:b, _!(:c)])} do
|
261
|
-
puts a.to_s + ", " + b.to_s + " and " + c.to_s
|
262
|
-
end
|
263
|
-
seems something do
|
264
|
-
puts "not matched"
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
# hash to hash match example
|
269
|
-
dict = build { {:name => "Taro", :age => 5} }
|
270
|
-
make dict do
|
271
|
-
seems as {{:name => :name}} do
|
272
|
-
puts "He is " + name
|
273
|
-
end
|
274
|
-
seems something do
|
275
|
-
puts "no name"
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
# hash to obj match example
|
280
|
-
class Person
|
281
|
-
def initialize(name, age)
|
282
|
-
@name = name
|
283
|
-
@age = age
|
284
|
-
end
|
285
|
-
attr :name
|
286
|
-
attr :age
|
287
|
-
end
|
288
|
-
|
289
|
-
make Person.new("Jiro", 3) do
|
290
|
-
seems as {{:name => :name}} do
|
291
|
-
puts "He is " + name
|
292
|
-
end
|
293
|
-
seems something do
|
294
|
-
puts "no name"
|
295
|
-
end
|
243
|
+
include DSL
|
296
244
|
end
|
297
|
-
=end
|
data/lib/patternmatching.rb
CHANGED
@@ -1,5 +1,111 @@
|
|
1
|
-
module PatternMatching
|
2
|
-
end
|
3
1
|
|
4
2
|
require 'patternmatching/version'
|
5
3
|
require 'patternmatching/pattern'
|
4
|
+
|
5
|
+
module PatternMatching
|
6
|
+
# The module is only for showing example docs.
|
7
|
+
#== Using module
|
8
|
+
# # If installed from rubygems
|
9
|
+
# require "rubygems"
|
10
|
+
# gem "patternmatching"
|
11
|
+
#
|
12
|
+
# # for use
|
13
|
+
# require "patternmatching"
|
14
|
+
#
|
15
|
+
#== Structured data example
|
16
|
+
# val = 200
|
17
|
+
# code = PatternMatching.build {plus(mul(100, 100), val)}
|
18
|
+
#
|
19
|
+
#== Partial func example
|
20
|
+
# class Calc
|
21
|
+
# extend PatternMatching
|
22
|
+
#
|
23
|
+
# func(:calcm).seems as {plus(:a, :b)} do
|
24
|
+
# calcm(a) + calcm(b)
|
25
|
+
# end
|
26
|
+
# func(:calcm).seems as {mul(:a, :b)} do
|
27
|
+
# calcm(a) * calcm(b)
|
28
|
+
# end
|
29
|
+
# func(:calcm).seems as {:value} do
|
30
|
+
# value
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# p Calc.new.calcm(code)
|
35
|
+
#
|
36
|
+
#== Another partial style method example
|
37
|
+
# class CalcX
|
38
|
+
# extend PatternMatching
|
39
|
+
#
|
40
|
+
# func(:calcx) do
|
41
|
+
# seems as {plus(:a, :b)} do
|
42
|
+
# calcx(a) + calcx(b)
|
43
|
+
# end
|
44
|
+
# seems as {mul(:a, :b)} do
|
45
|
+
# calcx(a) * calcx(b)
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# func(:calcx).seems as {:value} do
|
49
|
+
# value
|
50
|
+
# end
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# p CalcX.new.calcx(code)
|
54
|
+
#
|
55
|
+
#== Pattern matching example
|
56
|
+
# include PatternMatching
|
57
|
+
#
|
58
|
+
# def calc(code)
|
59
|
+
# make(code) {
|
60
|
+
# seems as {plus(:a, :b)} do calc(a) + calc(b) end
|
61
|
+
# seems as {mul(:a, :b)} do calc(a) * calc(b) end
|
62
|
+
# seems something do code end
|
63
|
+
# }
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# p calc(code)
|
67
|
+
#
|
68
|
+
#== Enumerable matching example
|
69
|
+
# is = build { exact([1,2,3,4,5]) }
|
70
|
+
# make is do
|
71
|
+
# seems as {exact([:a,:b, _!(:c)])} do
|
72
|
+
# puts a.to_s + ", " + b.to_s + " and " + c.to_s
|
73
|
+
# end
|
74
|
+
# seems something do
|
75
|
+
# puts "not matched"
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
#== Hash to Hash matching example
|
80
|
+
# dict = build { {:name => "Taro", :age => 5} }
|
81
|
+
# make dict do
|
82
|
+
# seems as {{:name => :name}} do
|
83
|
+
# puts "He is " + name
|
84
|
+
# end
|
85
|
+
# seems something do
|
86
|
+
# puts "no name"
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
#== Hash to non-Hash object matching example
|
91
|
+
# class Person
|
92
|
+
# def initialize(name, age)
|
93
|
+
# @name = name
|
94
|
+
# @age = age
|
95
|
+
# end
|
96
|
+
# attr :name
|
97
|
+
# attr :age
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# make Person.new("Jiro", 3) do
|
101
|
+
# seems as {{:name => :name}} do
|
102
|
+
# puts "He is " + name
|
103
|
+
# end
|
104
|
+
# seems something do
|
105
|
+
# puts "no name"
|
106
|
+
# end
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
module EXAMPLES
|
110
|
+
end
|
111
|
+
end
|
@@ -43,6 +43,26 @@ class Person
|
|
43
43
|
end
|
44
44
|
|
45
45
|
include PatternMatching
|
46
|
+
class PartialStyleDefs
|
47
|
+
def initialize
|
48
|
+
@name = "Foo"
|
49
|
+
end
|
50
|
+
attr :name
|
51
|
+
|
52
|
+
def bar
|
53
|
+
make "bar" do
|
54
|
+
seems as {:val} do
|
55
|
+
@name = val
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
func(:buzz).seems as {:val} do
|
61
|
+
@name = val
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
46
66
|
def calc(code)
|
47
67
|
make(code) {
|
48
68
|
seems as {plus(:a, :b)} do calc(a) + calc(b) end
|
@@ -159,4 +179,13 @@ describe "PatternMatching from Example" do
|
|
159
179
|
end
|
160
180
|
result.should == [120, 200]
|
161
181
|
end
|
182
|
+
|
183
|
+
it "should update fields from block" do
|
184
|
+
o = PartialStyleDefs.new
|
185
|
+
o.name.should == "Foo"
|
186
|
+
o.bar
|
187
|
+
o.name.should == "bar"
|
188
|
+
o.buzz("buzz")
|
189
|
+
o.name.should == "buzz"
|
190
|
+
end
|
162
191
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>PatternMatching module</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/patternmatching"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/patternmatching" class="numbers">0.1.
|
36
|
+
<a href="http://rubyforge.org/projects/patternmatching" class="numbers">0.1.3</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘patternmatching’</h1>
|
39
39
|
|
@@ -61,7 +61,7 @@ Provides a pure ruby module that:
|
|
61
61
|
|
62
62
|
|
63
63
|
<ul>
|
64
|
-
<li>For detail, see
|
64
|
+
<li>For detail, see <a href="http://en.wikipedia.org/wiki/Pattern_matching">Wikipedia: Pattern matching</a></li>
|
65
65
|
<li>Syntax based on meta-programming, like “rspec”, and so on.</li>
|
66
66
|
</ul>
|
67
67
|
|
@@ -86,6 +86,9 @@ to the following block when the pattern matched.</p>
|
|
86
86
|
|
87
87
|
<h3>Pattern matching expression</h3>
|
88
88
|
<pre>
|
89
|
+
# If installed from gem
|
90
|
+
# require "rubygems"
|
91
|
+
# gem "patternmatching"
|
89
92
|
require "patternmatching"
|
90
93
|
|
91
94
|
# For DSL style code, include PatternMatching
|
@@ -205,7 +208,7 @@ end # => "He is Jiro"
|
|
205
208
|
<h2>How to submit patches</h2>
|
206
209
|
|
207
210
|
|
208
|
-
<p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people’s code</a> and for section <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups">8b: Submit patch to Google Groups</a>, use the
|
211
|
+
<p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people’s code</a> and for section <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups">8b: Submit patch to Google Groups</a>, use the forum above.</p>
|
209
212
|
|
210
213
|
|
211
214
|
<p>The trunk repository is <code>svn://rubyforge.org/var/svn/patternmatching/trunk</code> for anonymous access.</p>
|
@@ -229,9 +232,21 @@ end # => "He is Jiro"
|
|
229
232
|
<h2>Contact</h2>
|
230
233
|
|
231
234
|
|
232
|
-
<p>Comments are welcome. Send
|
235
|
+
<p>Comments are welcome. Send a message to the forum.</p>
|
236
|
+
|
237
|
+
|
238
|
+
<h2>Authors</h2>
|
239
|
+
|
240
|
+
|
241
|
+
<ul>
|
242
|
+
<li><span class="caps">ICHIYAMA</span> Ryoichi: bellbind at gmail dot com</li>
|
243
|
+
</ul>
|
244
|
+
|
245
|
+
|
246
|
+
<p><a href="http://d.hatena.ne.jp/bellbind">My blog</a> (written in Japanese)
|
247
|
+
could be help you.</p>
|
233
248
|
<p class="coda">
|
234
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
249
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 5th June 2007<br>
|
235
250
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
236
251
|
</p>
|
237
252
|
</div>
|
data/website/index.txt
CHANGED
@@ -17,7 +17,7 @@ h2. The basics
|
|
17
17
|
|
18
18
|
This module provides methods for tree pattern matching features.
|
19
19
|
|
20
|
-
* For detail, see Wikipedia:
|
20
|
+
* For detail, see "Wikipedia: Pattern matching":http://en.wikipedia.org/wiki/Pattern_matching
|
21
21
|
* Syntax based on meta-programming, like "rspec", and so on.
|
22
22
|
|
23
23
|
Note: Default equivalence used in structured pattern matching is
|
@@ -36,6 +36,9 @@ to the following block when the pattern matched.
|
|
36
36
|
|
37
37
|
<h3>Pattern matching expression</h3>
|
38
38
|
<pre>
|
39
|
+
# If installed from gem
|
40
|
+
# require "rubygems"
|
41
|
+
# gem "patternmatching"
|
39
42
|
require "patternmatching"
|
40
43
|
|
41
44
|
# For DSL style code, include PatternMatching
|
@@ -152,7 +155,7 @@ Visit RubyForge project forum.
|
|
152
155
|
|
153
156
|
h2. How to submit patches
|
154
157
|
|
155
|
-
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the
|
158
|
+
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the forum above.
|
156
159
|
|
157
160
|
The trunk repository is <code>svn://rubyforge.org/var/svn/patternmatching/trunk</code> for anonymous access.
|
158
161
|
|
@@ -167,5 +170,11 @@ h2. Link
|
|
167
170
|
|
168
171
|
h2. Contact
|
169
172
|
|
170
|
-
Comments are welcome. Send
|
173
|
+
Comments are welcome. Send a message to the forum.
|
171
174
|
|
175
|
+
h2. Authors
|
176
|
+
|
177
|
+
* ICHIYAMA Ryoichi: bellbind at gmail dot com
|
178
|
+
|
179
|
+
"My blog":http://d.hatena.ne.jp/bellbind (written in Japanese)
|
180
|
+
could be help you.
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: patternmatching
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-06-
|
6
|
+
version: 0.1.3
|
7
|
+
date: 2007-06-05 00:00:00 +09:00
|
8
8
|
summary: Provide a pure ruby module that can build structured objects easily, can enable pattern match of objects, and can define method as a partial function style.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- Rakefile
|
37
37
|
- examples/enumerable_matching.rb
|
38
38
|
- examples/hash_matching.rb
|
39
|
+
- examples/match_inside_class.rb
|
39
40
|
- examples/matching.rb
|
40
41
|
- examples/object_matching.rb
|
41
42
|
- examples/partial_style_method.rb
|