egison 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -14
- data/lib/egison/core.rb +17 -3
- data/lib/egison/matcher-core.rb +42 -0
- data/lib/egison/matcher.rb +12 -52
- data/lib/egison/version.rb +1 -1
- data/sample/join.rb +7 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f225dc3acddccd5aea65c3db3ffb9affee8ce561
|
4
|
+
data.tar.gz: 2263fd6df2e9b1e0fbc1bf35b62bf1f264e5f7a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9193ade5c394260922f051ecd0fc6010ba3f54b9a902b0a631c4b7c416d0ce0eb26e6c380639d31704ef7f401c107aaded9e673198582315c16e75de9850fa72
|
7
|
+
data.tar.gz: a4816f03440ab863e5430a319607ff06e37265bbe51cbf52e50fee9801931a48765909ce4f6ca617376bf90cb58acd263c160f51e135e19f88be0d776e370961
|
data/README.md
CHANGED
@@ -87,31 +87,34 @@ We can write pattern-matching against lists, multisets, and sets.
|
|
87
87
|
When we regard an array as a multiset, the order of elements is ignored.
|
88
88
|
When we regard an array as a set, the duplicates and order of elements are ignored.
|
89
89
|
|
90
|
+
`__` is a <i>wildcard</i>.
|
91
|
+
It matches with any object.
|
92
|
+
|
90
93
|
```
|
91
94
|
match_all([1, 2, 3]) do
|
92
|
-
with(List.(_a, _b, *
|
95
|
+
with(List.(_a, _b, *__)) do
|
93
96
|
[a, b]
|
94
97
|
end
|
95
98
|
end #=> [[1, 2]]
|
96
99
|
|
97
100
|
match_all([1, 2, 3]) do
|
98
|
-
with(Multiset.(_a, _b, *
|
101
|
+
with(Multiset.(_a, _b, *__)) do
|
99
102
|
[a, b]
|
100
103
|
end
|
101
104
|
end #=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
|
102
105
|
|
103
106
|
match_all([1, 2, 3]) do
|
104
|
-
with(Set.(_a, _b, *
|
107
|
+
with(Set.(_a, _b, *__)) do
|
105
108
|
[a, b]
|
106
109
|
end
|
107
110
|
end #=> [[1, 1],[1, 2],[1, 3],[2, 1],[2, 2],[2, 3],[3, 1],[3, 2],[3, 3]]
|
108
111
|
```
|
109
112
|
|
110
|
-
Note that
|
113
|
+
Note that `_[]` is provided as syntactic sugar for `List.()`.
|
111
114
|
|
112
115
|
```
|
113
116
|
match_all([1, 2, 3]) do
|
114
|
-
with(_[_a, _b, *
|
117
|
+
with(_[_a, _b, *__]) do
|
115
118
|
[a, b]
|
116
119
|
end
|
117
120
|
end #=> [[1, 2]]
|
@@ -127,7 +130,7 @@ It matches the target when the target is equal with the value that `...` evaluat
|
|
127
130
|
|
128
131
|
```
|
129
132
|
match_all([5, 3, 4, 1, 2]) do
|
130
|
-
with(Multiset.(_a, __("a + 1"), __("a + 2"), *
|
133
|
+
with(Multiset.(_a, __("a + 1"), __("a + 2"), *__)) do
|
131
134
|
a
|
132
135
|
end
|
133
136
|
end #=> [1,2,3]
|
@@ -137,7 +140,7 @@ When, the expression in the place of `...` is a single variable, we can omit `("
|
|
137
140
|
|
138
141
|
```
|
139
142
|
match_all([1, 2, 3, 2, 5]) do
|
140
|
-
with(Multiset.(_a, __a, *
|
143
|
+
with(Multiset.(_a, __a, *__)) do
|
141
144
|
a
|
142
145
|
end
|
143
146
|
end #=> [2,2]
|
@@ -157,28 +160,28 @@ def poker_hands cs
|
|
157
160
|
with(Multiset.(_[_s, _n], _[__s, __("n+1")], _[__s, __("n+2")], _[__s, __("n+3")], _[__s, __("n+4")])) do
|
158
161
|
"Straight flush"
|
159
162
|
end
|
160
|
-
with(Multiset.(_[
|
163
|
+
with(Multiset.(_[__, _n], _[__, __n], _[__, __n], _[__, __n], __)) do
|
161
164
|
"Four of kind"
|
162
165
|
end
|
163
|
-
with(Multiset.(_[
|
166
|
+
with(Multiset.(_[__, _m], _[__, __m], _[__, __m], _[__, _n], _[__, __n])) do
|
164
167
|
"Full house"
|
165
168
|
end
|
166
169
|
with(Multiset.(_[_s, _], _[__s, _], _[__s, _], _[__s, _], _[__s, _])) do
|
167
170
|
"Flush"
|
168
171
|
end
|
169
|
-
with(Multiset.(_[
|
172
|
+
with(Multiset.(_[__, _n], _[__, __("n+1")], _[__, __("n+2")], _[__, __("n+3")], _[__, __("n+4")])) do
|
170
173
|
"Straight"
|
171
174
|
end
|
172
|
-
with(Multiset.(_[
|
175
|
+
with(Multiset.(_[__, _n], _[__, __n], _[__, __n], __, __)) do
|
173
176
|
"Three of kind"
|
174
177
|
end
|
175
|
-
with(Multiset.(_[
|
178
|
+
with(Multiset.(_[__, _m], _[__, __m], _[__, _n], _[__, __n], __)) do
|
176
179
|
"Two pairs"
|
177
180
|
end
|
178
|
-
with(Multiset.(_[
|
181
|
+
with(Multiset.(_[__, _n], _[__, __n], __, __, __)) do
|
179
182
|
"One pair"
|
180
183
|
end
|
181
|
-
with(Multiset.(
|
184
|
+
with(Multiset.(__, __, __, __, __)) do
|
182
185
|
"Nothing"
|
183
186
|
end
|
184
187
|
end
|
data/lib/egison/core.rb
CHANGED
@@ -264,10 +264,23 @@ module PatternMatch
|
|
264
264
|
end
|
265
265
|
end
|
266
266
|
|
267
|
-
def __(
|
268
|
-
|
267
|
+
def __(*vals)
|
268
|
+
case vals.length
|
269
|
+
when 0
|
270
|
+
uscore = Wildcard.new()
|
271
|
+
class << uscore
|
272
|
+
def [](*args)
|
273
|
+
List.call(*args)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
uscore
|
277
|
+
when 1
|
278
|
+
ValuePattern.new(@ctx, vals[0])
|
279
|
+
else
|
280
|
+
undefined
|
281
|
+
end
|
269
282
|
end
|
270
|
-
|
283
|
+
|
271
284
|
class BindingModule < ::Module
|
272
285
|
end
|
273
286
|
|
@@ -353,3 +366,4 @@ module Kernel
|
|
353
366
|
end
|
354
367
|
|
355
368
|
end
|
369
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'egison/core'
|
2
|
+
|
3
|
+
class Class
|
4
|
+
include PatternMatch::Matchable
|
5
|
+
|
6
|
+
def uncons(val)
|
7
|
+
raise NotImplementedError, "need to define `#{__method__}'"
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def accept_array_only(val)
|
13
|
+
raise PatternMatch::PatternNotMatch unless val.kind_of?(Array)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class List
|
18
|
+
end
|
19
|
+
|
20
|
+
class << List
|
21
|
+
def uncons(val)
|
22
|
+
accept_array_only(val)
|
23
|
+
val2 = val.clone
|
24
|
+
x = val2.shift
|
25
|
+
[[x, val2]]
|
26
|
+
end
|
27
|
+
|
28
|
+
def unjoin(val)
|
29
|
+
accept_array_only(val)
|
30
|
+
val2 = val.clone
|
31
|
+
xs = []
|
32
|
+
ys = val2.clone
|
33
|
+
rets = [[xs, ys]]
|
34
|
+
while !val2.empty? do
|
35
|
+
x = val2.shift
|
36
|
+
ys = val2.clone
|
37
|
+
xs = xs + [x]
|
38
|
+
rets = rets + [[xs, ys]]
|
39
|
+
end
|
40
|
+
rets
|
41
|
+
end
|
42
|
+
end
|
data/lib/egison/matcher.rb
CHANGED
@@ -1,45 +1,5 @@
|
|
1
1
|
require 'egison/core'
|
2
|
-
|
3
|
-
class Class
|
4
|
-
include PatternMatch::Matchable
|
5
|
-
|
6
|
-
def uncons(val)
|
7
|
-
raise NotImplementedError, "need to define `#{__method__}'"
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
|
12
|
-
def accept_array_only(val)
|
13
|
-
raise PatternMatch::PatternNotMatch unless val.kind_of?(Array)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class List
|
18
|
-
end
|
19
|
-
|
20
|
-
class << List
|
21
|
-
def uncons(val)
|
22
|
-
accept_array_only(val)
|
23
|
-
val2 = val.clone
|
24
|
-
x = val2.shift
|
25
|
-
[[x, val2]]
|
26
|
-
end
|
27
|
-
|
28
|
-
def unjoin(val)
|
29
|
-
accept_array_only(val)
|
30
|
-
val2 = val.clone
|
31
|
-
xs = []
|
32
|
-
ys = val2.clone
|
33
|
-
rets = [[xs, ys]]
|
34
|
-
while !val2.empty? do
|
35
|
-
x = val2.shift
|
36
|
-
ys = val2.clone
|
37
|
-
xs = xs + [x]
|
38
|
-
rets = rets + [[xs, ys]]
|
39
|
-
end
|
40
|
-
rets
|
41
|
-
end
|
42
|
-
end
|
2
|
+
require 'egison/matcher-core'
|
43
3
|
|
44
4
|
class Multiset
|
45
5
|
end
|
@@ -47,12 +7,11 @@ end
|
|
47
7
|
class << Multiset
|
48
8
|
def uncons(val)
|
49
9
|
accept_array_only(val)
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
rets
|
10
|
+
match_all(val) do
|
11
|
+
with(List.(*_hs, _x, *_ts)) do
|
12
|
+
[x, hs + ts]
|
13
|
+
end
|
14
|
+
end
|
56
15
|
end
|
57
16
|
|
58
17
|
def unjoin(val)
|
@@ -79,12 +38,13 @@ end
|
|
79
38
|
class << Set
|
80
39
|
def uncons(val)
|
81
40
|
accept_array_only(val)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
41
|
+
match_all(val) do
|
42
|
+
with(List.(*_, _x, *_)) do
|
43
|
+
[x, val]
|
44
|
+
end
|
45
|
+
end
|
87
46
|
end
|
47
|
+
|
88
48
|
def unjoin(val)
|
89
49
|
accept_array_only(val)
|
90
50
|
val2 = val.clone
|
data/lib/egison/version.rb
CHANGED
data/sample/join.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: egison
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Satoshi Egi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -40,9 +40,11 @@ files:
|
|
40
40
|
- egison.gemspec
|
41
41
|
- lib/egison.rb
|
42
42
|
- lib/egison/core.rb
|
43
|
+
- lib/egison/matcher-core.rb
|
43
44
|
- lib/egison/matcher.rb
|
44
45
|
- lib/egison/version.rb
|
45
46
|
- sample/combination.rb
|
47
|
+
- sample/join.rb
|
46
48
|
- sample/poker_hands.rb
|
47
49
|
- sample/set.rb
|
48
50
|
homepage: https://github.com/egisatoshi/egison-ruby
|