patternmatching 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +2 -0
- data/README.txt +19 -0
- data/examples/matching_with_condition.rb +16 -0
- data/examples/overhead.rb +68 -0
- data/lib/patternmatching/pattern.rb +21 -7
- data/lib/patternmatching/version.rb +1 -1
- data/spec/patternmatching_spec.rb +9 -0
- data/website/index.html +22 -2
- data/website/index.txt +19 -0
- metadata +4 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -7,7 +7,9 @@ examples/enumerable_matching.rb
|
|
7
7
|
examples/hash_matching.rb
|
8
8
|
examples/match_inside_class.rb
|
9
9
|
examples/matching.rb
|
10
|
+
examples/matching_with_condition.rb
|
10
11
|
examples/object_matching.rb
|
12
|
+
examples/overhead.rb
|
11
13
|
examples/partial_style_method.rb
|
12
14
|
examples/partial_style_method2.rb
|
13
15
|
lib/patternmatching.rb
|
data/README.txt
CHANGED
@@ -179,6 +179,25 @@ It is ver. 0.2.0 API
|
|
179
179
|
p o.buzz("buzz") == o #=> true
|
180
180
|
p o.name #=> "buzz"
|
181
181
|
|
182
|
+
=== Pattern with Condition Example
|
183
|
+
It is from ver. 0.2.2 API
|
184
|
+
|
185
|
+
require "patternmatching"
|
186
|
+
|
187
|
+
include PatternMatching
|
188
|
+
|
189
|
+
num = 100
|
190
|
+
result = make num do
|
191
|
+
seems as {:n}, with {n < 10} do
|
192
|
+
true
|
193
|
+
end
|
194
|
+
seems as {:n}, with {n >= 10} do
|
195
|
+
false
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
p result #=> false
|
200
|
+
|
182
201
|
== Forum
|
183
202
|
|
184
203
|
Visit the project forum in RubyForge.
|
@@ -0,0 +1,68 @@
|
|
1
|
+
|
2
|
+
require "patternmatching"
|
3
|
+
|
4
|
+
include PatternMatching
|
5
|
+
|
6
|
+
class Through
|
7
|
+
def plain(n)
|
8
|
+
n
|
9
|
+
end
|
10
|
+
|
11
|
+
func(:pattern).seems as {:n} do n end
|
12
|
+
end
|
13
|
+
|
14
|
+
count = 10000
|
15
|
+
|
16
|
+
plain = Through.new
|
17
|
+
start = Time.new
|
18
|
+
count.times do |i|
|
19
|
+
plain.plain(i)
|
20
|
+
end
|
21
|
+
plain_time = Time.new - start
|
22
|
+
|
23
|
+
pattern = Through.new
|
24
|
+
start = Time.new
|
25
|
+
count.times do |i|
|
26
|
+
pattern.pattern(i)
|
27
|
+
end
|
28
|
+
pattern_time = Time.new - start
|
29
|
+
|
30
|
+
puts "Check Through call " + count.to_s + " times"
|
31
|
+
puts "Standard Method cost: " + plain_time.to_s + " sec"
|
32
|
+
puts "Partial Method cost: " + pattern_time.to_s + " sec"
|
33
|
+
|
34
|
+
puts "Overhead/method: " +
|
35
|
+
((pattern_time - plain_time).to_f / count * 1000).to_s + " msec"
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
class Recursive
|
40
|
+
def plain(n)
|
41
|
+
if n > 0
|
42
|
+
plain(n - 1)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
func(:pattern).seems as {:n}, with {n > 0} do
|
47
|
+
pattern(n-1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
count = 300 # depends on system stack size
|
52
|
+
|
53
|
+
plain = Recursive.new
|
54
|
+
start = Time.new
|
55
|
+
plain.plain(count)
|
56
|
+
plain_time = Time.new - start
|
57
|
+
|
58
|
+
pattern = Recursive.new
|
59
|
+
start = Time.new
|
60
|
+
pattern.pattern(count)
|
61
|
+
pattern_time = Time.new - start
|
62
|
+
|
63
|
+
puts "Check Recursive call " + count.to_s + " times"
|
64
|
+
puts "Standard Method cost: " + plain_time.to_s + " sec"
|
65
|
+
puts "Partial Method cost: " + pattern_time.to_s + " sec"
|
66
|
+
puts "Overhead/method: " +
|
67
|
+
((pattern_time - plain_time).to_f / count * 1000).to_s + " msec"
|
68
|
+
|
@@ -107,9 +107,10 @@ module PatternMatching
|
|
107
107
|
#Private class to run pattern matching
|
108
108
|
class MatchExec
|
109
109
|
def self.exec_as(target, patterns, receiver)
|
110
|
-
patterns.each do |
|
111
|
-
pattern =
|
112
|
-
action =
|
110
|
+
patterns.each do |tuple|
|
111
|
+
pattern = tuple[0]
|
112
|
+
action = tuple[1]
|
113
|
+
condition = tuple[2]
|
113
114
|
source = NodeBuilder.new.instance_eval(&pattern)
|
114
115
|
args = {}
|
115
116
|
begin
|
@@ -117,8 +118,11 @@ module PatternMatching
|
|
117
118
|
rescue NotMatched
|
118
119
|
next
|
119
120
|
end
|
120
|
-
|
121
|
-
|
121
|
+
executer = ExecuteAs.new(args, receiver)
|
122
|
+
next if condition != nil and not executer.instance_eval(&condition)
|
123
|
+
return executer.instance_eval(&action)
|
124
|
+
end
|
125
|
+
nil
|
122
126
|
end
|
123
127
|
|
124
128
|
# Private class to access instance valiables of the receiver
|
@@ -167,13 +171,16 @@ module PatternMatching
|
|
167
171
|
def initialize(patterns)
|
168
172
|
@patterns = patterns
|
169
173
|
end
|
170
|
-
def seems(pattern, &action)
|
171
|
-
@patterns <<
|
174
|
+
def seems(pattern, condition = nil, &action)
|
175
|
+
@patterns << [pattern, action, condition]
|
172
176
|
self
|
173
177
|
end
|
174
178
|
def as(&block)
|
175
179
|
block
|
176
180
|
end
|
181
|
+
def with(&block)
|
182
|
+
block
|
183
|
+
end
|
177
184
|
def something
|
178
185
|
proc {_}
|
179
186
|
end
|
@@ -215,6 +222,13 @@ module PatternMatching
|
|
215
222
|
block
|
216
223
|
end
|
217
224
|
|
225
|
+
#A pattern restriction, must boolean
|
226
|
+
#=== Usage
|
227
|
+
# seems as {PATTERN}, with {CONDITION} do ... end
|
228
|
+
def with(&block)
|
229
|
+
block
|
230
|
+
end
|
231
|
+
|
218
232
|
#A pattern matches anything
|
219
233
|
#=== Usage
|
220
234
|
# seems something do ... end
|
@@ -195,4 +195,13 @@ describe "PatternMatching from Example" do
|
|
195
195
|
o.buzz("buzz").should == o
|
196
196
|
o.name.should == "buzz"
|
197
197
|
end
|
198
|
+
|
199
|
+
it "should check pattern with condition" do
|
200
|
+
make(100) do
|
201
|
+
seems as {:a}, with {a > 10} do a end
|
202
|
+
end.should == 100
|
203
|
+
make(1) do
|
204
|
+
seems as {:a}, with {a > 10} do a end
|
205
|
+
end.should == nil
|
206
|
+
end
|
198
207
|
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.2.
|
36
|
+
<a href="http://rubyforge.org/projects/patternmatching" class="numbers">0.2.2</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘patternmatching’</h1>
|
39
39
|
|
@@ -235,6 +235,26 @@ p o.bar == o #=> true
|
|
235
235
|
p o.name #=> "bar"
|
236
236
|
p o.buzz("buzz") == o #=> true
|
237
237
|
p o.name #=> "buzz"
|
238
|
+
</pre>
|
239
|
+
|
240
|
+
<h3>Pattern with Condition Example</h3>
|
241
|
+
It is from ver 0.2.2 <span class="caps">API</span>
|
242
|
+
<pre>
|
243
|
+
require "patternmatching"
|
244
|
+
|
245
|
+
include PatternMatching
|
246
|
+
|
247
|
+
num = 100
|
248
|
+
result = make num do
|
249
|
+
seems as {:n}, with {n < 10} do
|
250
|
+
true
|
251
|
+
end
|
252
|
+
seems as {:n}, with {n >= 10} do
|
253
|
+
false
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
p result #=> false
|
238
258
|
</pre>
|
239
259
|
|
240
260
|
<h2>Forum</h2>
|
@@ -285,7 +305,7 @@ p o.name #=> "buzz"
|
|
285
305
|
<p><a href="http://d.hatena.ne.jp/bellbind/">My blog</a> (written in Japanese)
|
286
306
|
could be help you.</p>
|
287
307
|
<p class="coda">
|
288
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
308
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 6th June 2007<br>
|
289
309
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
290
310
|
</p>
|
291
311
|
</div>
|
data/website/index.txt
CHANGED
@@ -187,6 +187,25 @@ p o.buzz("buzz") == o #=> true
|
|
187
187
|
p o.name #=> "buzz"
|
188
188
|
</pre>
|
189
189
|
|
190
|
+
<h3>Pattern with Condition Example</h3>
|
191
|
+
It is from ver 0.2.2 API
|
192
|
+
<pre>
|
193
|
+
require "patternmatching"
|
194
|
+
|
195
|
+
include PatternMatching
|
196
|
+
|
197
|
+
num = 100
|
198
|
+
result = make num do
|
199
|
+
seems as {:n}, with {n < 10} do
|
200
|
+
true
|
201
|
+
end
|
202
|
+
seems as {:n}, with {n >= 10} do
|
203
|
+
false
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
p result #=> false
|
208
|
+
</pre>
|
190
209
|
|
191
210
|
h2. Forum
|
192
211
|
|
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.2.
|
7
|
-
date: 2007-06-
|
6
|
+
version: 0.2.2
|
7
|
+
date: 2007-06-06 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
|
@@ -38,7 +38,9 @@ files:
|
|
38
38
|
- examples/hash_matching.rb
|
39
39
|
- examples/match_inside_class.rb
|
40
40
|
- examples/matching.rb
|
41
|
+
- examples/matching_with_condition.rb
|
41
42
|
- examples/object_matching.rb
|
43
|
+
- examples/overhead.rb
|
42
44
|
- examples/partial_style_method.rb
|
43
45
|
- examples/partial_style_method2.rb
|
44
46
|
- lib/patternmatching.rb
|