egison 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f225dc3acddccd5aea65c3db3ffb9affee8ce561
4
- data.tar.gz: 2263fd6df2e9b1e0fbc1bf35b62bf1f264e5f7a3
3
+ metadata.gz: 93830b57e70db21c121811db9cdb6dc290c8f1d4
4
+ data.tar.gz: f03a022950af1034baa7f5f05ec8c46bbcdfd27e
5
5
  SHA512:
6
- metadata.gz: 9193ade5c394260922f051ecd0fc6010ba3f54b9a902b0a631c4b7c416d0ce0eb26e6c380639d31704ef7f401c107aaded9e673198582315c16e75de9850fa72
7
- data.tar.gz: a4816f03440ab863e5430a319607ff06e37265bbe51cbf52e50fee9801931a48765909ce4f6ca617376bf90cb58acd263c160f51e135e19f88be0d776e370961
6
+ metadata.gz: 6dc21dd3b11e12c3db43dfec0631f922eb9da0e6873dbcc055777801746ba0d97a49d2fba8da89965044eaadda57aa3487930a2880834dc65b8f559b76638522
7
+ data.tar.gz: 39c8fc34bfb2533991fa7b896a660523107a1a58fb51015d4b98b92228c90cbdffce909136e3ae9d5ca3e9c30b47d49bd90b8208425f392ac07f121f791968e4
data/Makefile CHANGED
@@ -1,3 +1,6 @@
1
1
  all:
2
2
  gem build egison.gemspec
3
3
  gem install egison-*.gem
4
+
5
+ test:
6
+ bundle exec rspec spec
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
- # The Gem for Egison Pattern Matching
1
+ # The Gem for Egison Pattern Matching
2
2
 
3
- This Gem provides a way to access Egison pattern-matching from Ruby.
4
- Egison is the world's first programming language that can represent non-linear pattern-match against unfree data types.
3
+ This Gem provides a way to access non-linear pattern-matching against unfree data types from Ruby.
5
4
  We can directly express pattern-matching against lists, multisets, and sets using this gem.
6
5
 
7
6
  ## Installation
@@ -15,8 +14,7 @@ or
15
14
  ```
16
15
  $ git clone https://github.com/egison/egison-ruby.git
17
16
  $ cd egison-ruby
18
- $ gem build egison.gemspec
19
- $ gem install egison-*.gem
17
+ $ make
20
18
  ```
21
19
 
22
20
  or
@@ -24,7 +22,7 @@ or
24
22
  ```
25
23
  $ gem install bundler (if you need)
26
24
  $ echo "gem 'egison', :git => 'https://github.com/egison/egison-ruby.git'" > Gemfile
27
- $ bundle install --path vendor/bundle
25
+ $ bundle install
28
26
  ```
29
27
 
30
28
  ## Basic Usage
@@ -87,24 +85,26 @@ We can write pattern-matching against lists, multisets, and sets.
87
85
  When we regard an array as a multiset, the order of elements is ignored.
88
86
  When we regard an array as a set, the duplicates and order of elements are ignored.
89
87
 
90
- `__` is a <i>wildcard</i>.
88
+ `_` is a <i>wildcard</i>.
91
89
  It matches with any object.
90
+ Note that `__` and `___` are also interpreted as a wildcard.
91
+ This is because `_` and `__` are system variables and sometimes have its own meaning.
92
92
 
93
93
  ```
94
94
  match_all([1, 2, 3]) do
95
- with(List.(_a, _b, *__)) do
95
+ with(List.(_a, _b, *_)) do
96
96
  [a, b]
97
97
  end
98
98
  end #=> [[1, 2]]
99
99
 
100
100
  match_all([1, 2, 3]) do
101
- with(Multiset.(_a, _b, *__)) do
101
+ with(Multiset.(_a, _b, *_)) do
102
102
  [a, b]
103
103
  end
104
104
  end #=> [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
105
105
 
106
106
  match_all([1, 2, 3]) do
107
- with(Set.(_a, _b, *__)) do
107
+ with(Set.(_a, _b, *_)) do
108
108
  [a, b]
109
109
  end
110
110
  end #=> [[1, 1],[1, 2],[1, 3],[2, 1],[2, 2],[2, 3],[3, 1],[3, 2],[3, 3]]
@@ -114,7 +114,7 @@ Note that `_[]` is provided as syntactic sugar for `List.()`.
114
114
 
115
115
  ```
116
116
  match_all([1, 2, 3]) do
117
- with(_[_a, _b, *__]) do
117
+ with(_[_a, _b, *_]) do
118
118
  [a, b]
119
119
  end
120
120
  end #=> [[1, 2]]
@@ -130,7 +130,7 @@ It matches the target when the target is equal with the value that `...` evaluat
130
130
 
131
131
  ```
132
132
  match_all([5, 3, 4, 1, 2]) do
133
- with(Multiset.(_a, __("a + 1"), __("a + 2"), *__)) do
133
+ with(Multiset.(_a, __("a + 1"), __("a + 2"), *_)) do
134
134
  a
135
135
  end
136
136
  end #=> [1,2,3]
@@ -140,13 +140,30 @@ When, the expression in the place of `...` is a single variable, we can omit `("
140
140
 
141
141
  ```
142
142
  match_all([1, 2, 3, 2, 5]) do
143
- with(Multiset.(_a, __a, *__)) do
143
+ with(Multiset.(_a, __a, *_)) do
144
144
  a
145
145
  end
146
146
  end #=> [2,2]
147
147
  ```
148
148
 
149
- ## Demonstration - Poker Hands
149
+ ## Demonstrations
150
+
151
+ ### Combinations
152
+
153
+ We can enumerates all combinations of the elements of a collection with pattern-matching.
154
+
155
+ ```
156
+ require 'egison'
157
+
158
+ p(match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_)) { [x, y] } end)
159
+ #=> [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
160
+
161
+ p(match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_, _z, *_)) { [x, y, z] } end)
162
+ #=> [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
163
+
164
+ ```
165
+
166
+ ### Poker Hands
150
167
 
151
168
  We can write patterns for all poker-hands in one single pattern.
152
169
  It is as follow.
@@ -160,28 +177,28 @@ def poker_hands cs
160
177
  with(Multiset.(_[_s, _n], _[__s, __("n+1")], _[__s, __("n+2")], _[__s, __("n+3")], _[__s, __("n+4")])) do
161
178
  "Straight flush"
162
179
  end
163
- with(Multiset.(_[__, _n], _[__, __n], _[__, __n], _[__, __n], __)) do
180
+ with(Multiset.(_[_, _n], _[_, __n], _[_, __n], _[_, __n], _)) do
164
181
  "Four of kind"
165
182
  end
166
- with(Multiset.(_[__, _m], _[__, __m], _[__, __m], _[__, _n], _[__, __n])) do
183
+ with(Multiset.(_[_, _m], _[_, __m], _[_, __m], _[_, _n], _[_, __n])) do
167
184
  "Full house"
168
185
  end
169
186
  with(Multiset.(_[_s, _], _[__s, _], _[__s, _], _[__s, _], _[__s, _])) do
170
187
  "Flush"
171
188
  end
172
- with(Multiset.(_[__, _n], _[__, __("n+1")], _[__, __("n+2")], _[__, __("n+3")], _[__, __("n+4")])) do
189
+ with(Multiset.(_[_, _n], _[_, __("n+1")], _[_, __("n+2")], _[_, __("n+3")], _[_, __("n+4")])) do
173
190
  "Straight"
174
191
  end
175
- with(Multiset.(_[__, _n], _[__, __n], _[__, __n], __, __)) do
192
+ with(Multiset.(_[_, _n], _[_, __n], _[_, __n], _, _)) do
176
193
  "Three of kind"
177
194
  end
178
- with(Multiset.(_[__, _m], _[__, __m], _[__, _n], _[__, __n], __)) do
195
+ with(Multiset.(_[_, _m], _[_, __m], _[_, _n], _[_, __n], _)) do
179
196
  "Two pairs"
180
197
  end
181
- with(Multiset.(_[__, _n], _[__, __n], __, __, __)) do
198
+ with(Multiset.(_[_, _n], _[_, __n], _, _, _)) do
182
199
  "One pair"
183
200
  end
184
- with(Multiset.(__, __, __, __, __)) do
201
+ with(Multiset.(_, _, _, _, _)) do
185
202
  "Nothing"
186
203
  end
187
204
  end
data/egison.gemspec CHANGED
@@ -17,5 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
18
18
  s.require_paths = ['lib']
19
19
  s.add_development_dependency 'rake'
20
+ s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'simplecov'
20
22
  s.rdoc_options = ['--main', 'README.rdoc']
21
23
  end
data/lib/egison/core.rb CHANGED
@@ -257,8 +257,6 @@ module PatternMatch
257
257
  end
258
258
  end
259
259
  uscore
260
- when 1
261
- ValuePattern.new(@ctx, vals[0])
262
260
  else
263
261
  undefined
264
262
  end
@@ -267,13 +265,7 @@ module PatternMatch
267
265
  def __(*vals)
268
266
  case vals.length
269
267
  when 0
270
- uscore = Wildcard.new()
271
- class << uscore
272
- def [](*args)
273
- List.call(*args)
274
- end
275
- end
276
- uscore
268
+ Wildcard.new()
277
269
  when 1
278
270
  ValuePattern.new(@ctx, vals[0])
279
271
  else
@@ -281,6 +273,15 @@ module PatternMatch
281
273
  end
282
274
  end
283
275
 
276
+ def ___(*vals)
277
+ case vals.length
278
+ when 0
279
+ Wildcard.new()
280
+ else
281
+ undefined
282
+ end
283
+ end
284
+
284
285
  class BindingModule < ::Module
285
286
  end
286
287
 
@@ -364,6 +365,8 @@ module Kernel
364
365
  env.instance_eval(&block)
365
366
  end
366
367
  end
368
+
369
+ alias match_single match
367
370
 
368
371
  end
369
372
 
@@ -1,3 +1,3 @@
1
1
  module Egison
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ require 'spec_helper'
2
+ require 'egison/core'
3
+
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+ require 'egison/core'
3
+ require 'egison/matcher'
4
+
@@ -0,0 +1,3 @@
1
+ require 'spec_helper'
2
+ require 'egison'
3
+
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'egison'
3
+
4
+ describe "sample" do
5
+ describe "combination.rb" do
6
+ it %q{match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_)) { [x, y] } end} do
7
+ expect(match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_)) { [x, y] } end).to eq \
8
+ [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
9
+ end
10
+ it %q{match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_, _z, *_)) { [x, y, z] } end} do
11
+ expect(match_all([1,2,3,4,5]) do with(List.(*_, _x, *_, _y, *_, _z, *_)) { [x, y, z] } end).to eq \
12
+ [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'egison'
3
+
4
+ describe "sample" do
5
+ describe "join.rb" do
6
+ it %q{match_all([1,2,3,4,5]) do with(List.(*_hs, *_ts)) { [hs, ts] } end} do
7
+ expect(match_all([1,2,3,4,5]) do with(List.(*_hs, *_ts)) { [hs, ts] } end).to eq \
8
+ [[[], [1, 2, 3, 4, 5]], [[1], [2, 3, 4, 5]], [[1, 2], [3, 4, 5]], [[1, 2, 3], [4, 5]], [[1, 2, 3, 4], [5]], [[1, 2, 3, 4, 5], []]]
9
+ end
10
+ it %q{match_all([1,2,3,4,5]) do with(Multiset.(*_hs, *_ts)) { [hs, ts] } end} do
11
+ expect(match_all([1,2,3,4,5]) do with(Multiset.(*_hs, *_ts)) { [hs, ts] } end).to eq \
12
+ [[[], [1, 2, 3, 4, 5]], [[5], [1, 2, 3, 4]], [[4], [1, 2, 3, 5]], [[4, 5], [1, 2, 3]], [[3], [1, 2, 4, 5]], [[3, 5], [1, 2, 4]], [[3, 4], [1, 2, 5]], [[3, 4, 5], [1, 2]], [[2], [1, 3, 4, 5]], [[2, 5], [1, 3, 4]], [[2, 4], [1, 3, 5]], [[2, 4, 5], [1, 3]], [[2, 3], [1, 4, 5]], [[2, 3, 5], [1, 4]], [[2, 3, 4], [1, 5]], [[2, 3, 4, 5], [1]], [[1], [2, 3, 4, 5]], [[1, 5], [2, 3, 4]], [[1, 4], [2, 3, 5]], [[1, 4, 5], [2, 3]], [[1, 3], [2, 4, 5]], [[1, 3, 5], [2, 4]], [[1, 3, 4], [2, 5]], [[1, 3, 4, 5], [2]], [[1, 2], [3, 4, 5]], [[1, 2, 5], [3, 4]], [[1, 2, 4], [3, 5]], [[1, 2, 4, 5], [3]], [[1, 2, 3], [4, 5]], [[1, 2, 3, 5], [4]], [[1, 2, 3, 4], [5]], [[1, 2, 3, 4, 5], []]]
13
+ end
14
+ it %q{match_all([1,2,3,4,5]) do with(Set.(*_hs, *_ts)) { [hs, ts] } end} do
15
+ expect(match_all([1,2,3,4,5]) do with(Set.(*_hs, *_ts)) { [hs, ts] } end).to eq \
16
+ [[[], [1, 2, 3, 4, 5]], [[5], [1, 2, 3, 4, 5]], [[4], [1, 2, 3, 4, 5]], [[4, 5], [1, 2, 3, 4, 5]], [[3], [1, 2, 3, 4, 5]], [[3, 5], [1, 2, 3, 4, 5]], [[3, 4], [1, 2, 3, 4, 5]], [[3, 4, 5], [1, 2, 3, 4, 5]], [[2], [1, 2, 3, 4, 5]], [[2, 5],[1, 2, 3, 4, 5]], [[2, 4], [1, 2, 3, 4, 5]], [[2, 4, 5], [1, 2, 3, 4, 5]], [[2,3], [1, 2, 3, 4, 5]], [[2, 3, 5], [1, 2, 3, 4, 5]], [[2, 3, 4], [1, 2, 3, 4, 5]], [[2, 3, 4, 5], [1, 2, 3, 4, 5]], [[1], [1, 2, 3, 4, 5]], [[1, 5], [1, 2, 3, 4, 5]], [[1, 4], [1, 2, 3, 4, 5]], [[1, 4, 5], [1, 2, 3, 4, 5]], [[1, 3], [1, 2, 3, 4, 5]], [[1, 3, 5], [1, 2, 3, 4, 5]], [[1, 3, 4], [1, 2, 3, 4, 5]], [[1, 3, 4, 5], [1, 2, 3, 4, 5]], [[1, 2], [1, 2, 3, 4, 5]], [[1, 2, 5], [1, 2, 3, 4, 5]],[[1, 2, 4], [1, 2, 3, 4, 5]], [[1, 2, 4, 5], [1, 2, 3, 4, 5]], [[1, 2, 3], [1, 2, 3, 4, 5]], [[1, 2, 3, 5], [1, 2, 3, 4, 5]], [[1, 2, 3, 4], [1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'egison'
3
+
4
+ def poker_hands cs
5
+ match_single(cs) do
6
+ with(Multiset.(_[_s, _n], _[__s, __("n+1")], _[__s, __("n+2")], _[__s, __("n+3")], _[__s, __("n+4")])) do
7
+ "Straight flush"
8
+ end
9
+ with(Multiset.(_[_, _n], _[_, __n], _[_, __n], _[_, __n], _)) do
10
+ "Four of kind"
11
+ end
12
+ with(Multiset.(_[_, _m], _[_, __m], _[_, __m], _[_, _n], _[_, __n])) do
13
+ "Full house"
14
+ end
15
+ with(Multiset.(_[_s, _], _[__s, _], _[__s, _], _[__s, _], _[__s, _])) do
16
+ "Flush"
17
+ end
18
+ with(Multiset.(_[_, _n], _[_, __("n+1")], _[_, __("n+2")], _[_, __("n+3")], _[_, __("n+4")])) do
19
+ "Straight"
20
+ end
21
+ with(Multiset.(_[_, _n], _[_, __n], _[_, __n], _, _)) do
22
+ "Three of kind"
23
+ end
24
+ with(Multiset.(_[_, _m], _[_, __m], _[_, _n], _[_, __n], _)) do
25
+ "Two pairs"
26
+ end
27
+ with(Multiset.(_[_, _n], _[_, __n], _, _, _)) do
28
+ "One pair"
29
+ end
30
+ with(Multiset.(_, _, _, _, _)) do
31
+ "Nothing"
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "sample" do
37
+ describe "poker.rb" do
38
+ it "Straight flush" do
39
+ expect(poker_hands([["diamond", 1], ["diamond", 3], ["diamond", 5], ["diamond", 4], ["diamond", 2]])).to eq "Straight flush"
40
+ end
41
+ it "Full house" do
42
+ expect(poker_hands([["diamond", 1], ["club", 2], ["club", 1], ["heart", 1], ["diamond", 2]])).to eq "Full house"
43
+ end
44
+ it "Straight" do
45
+ expect(poker_hands([["diamond", 4], ["club", 2], ["club", 5], ["heart", 1], ["diamond", 3]])).to eq "Straight"
46
+ end
47
+ it "Nothing" do
48
+ expect(poker_hands([["diamond", 4], ["club", 10], ["club", 5], ["heart", 1], ["diamond", 3]])).to eq "Nothing"
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'egison'
3
+
4
+ describe "sample" do
5
+ describe "set.rb" do
6
+ it %q{match_all([1,2,3,4,5]) do with(Set.(_x,_y, *_)) { [x, y] } end} do
7
+ expect(match_all([1,2,3,4,5]) do with(Set.(_x,_y, *_)) { [x, y] } end).to eq \
8
+ [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ end
6
+
7
+ RSpec.configure do |config|
8
+ end
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.1.0
4
+ version: 0.2.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-14 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: The library to access Egison pattern-matching from Ruby.
28
56
  email:
29
57
  - egi@egison.org
@@ -47,6 +75,14 @@ files:
47
75
  - sample/join.rb
48
76
  - sample/poker_hands.rb
49
77
  - sample/set.rb
78
+ - spec/lib/egison/core_spec.rb
79
+ - spec/lib/egison/matcher_spec.rb
80
+ - spec/lib/egison_spec.rb
81
+ - spec/sample/combination_spec.rb
82
+ - spec/sample/join_spec.rb
83
+ - spec/sample/poker_hands_spec.rb
84
+ - spec/sample/set_spec.rb
85
+ - spec/spec_helper.rb
50
86
  homepage: https://github.com/egisatoshi/egison-ruby
51
87
  licenses: []
52
88
  metadata: {}