rparsec 0.4.1 → 0.4.2

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.
@@ -1,249 +1,270 @@
1
- #
2
- # This module provides frequently used functors.
3
- #
4
- module Functors
5
- Id = proc {|x|x}
6
- Idn = proc {|*x|x}
7
- Neg = proc {|x|-x}
8
- Inc = proc {|x|x+1}
9
- Dec = proc {|x|x-1}
10
- Plus = proc {|x,y|x+y}
11
- Minus = proc {|x,y|x-y}
12
- Mul = proc {|x,y|x*y}
13
- Div = proc {|x,y|x/y}
14
- Mod = proc {|x,y|x%y}
15
- Power = proc {|x,y|x**y}
16
- Not = proc {|x,y|!x}
17
- And = proc {|x,y|x&&y}
18
- Or = proc {|x,y|x||y}
19
- Xor = proc {|x,y|x^y}
20
- BitAnd = proc {|x,y|x&y}
21
- Union = proc {|x,y|x|y}
22
- Match = proc {|x,y|x=~y}
23
- Eq = proc {|x,y|x==y}
24
- Ne = proc {|x,y|x!=y}
25
- Lt = proc {|x,y|x<y}
26
- Gt = proc {|x,y|x>y}
27
- Le = proc {|x,y|x<=y}
28
- Ge = proc {|x,y|x>=y}
29
- Compare = proc {|x,y|x<=>y}
30
- Call = proc {|x,y|x.call(y)}
31
- Feed = proc {|x,y|y.call(x)}
32
- Fst = proc {|x,_|x}
33
- Snd = proc {|_, x|x}
34
- At = proc {|x,y|x[y]}
35
- To_a = proc {|x|x.to_a}
36
- To_s = proc {|x|x.to_s}
37
- To_i = proc {|x|x.to_i}
38
- To_sym = proc {|x|x.to_sym}
39
- To_f = proc {|x|x.to_f}
40
- #
41
- # Get a Proc, when called, always return the given value.
42
- #
43
- def const(v)
44
- proc {|_|v}
45
- end
46
- #
47
- # Get a Proc, when called, return the nth parameter.
48
- #
49
- def nth(n)
50
- proc {|*args|args[n]}
51
- end
52
-
53
-
54
- #
55
- # Create a Proc, which expects the two parameters
56
- # in the reverse order of _block_.
57
- #
58
- def flip(&block)
59
- proc {|x,y|block.call(y,x)}
60
- end
61
- #
62
- # Create a Proc, when called, the parameter is
63
- # first passed into _f2_, _f1_ is called in turn
64
- # with the return value from _other_.
65
- #
66
- def compose(f1, f2)
67
- proc {|*x|f1.call(f2.call(*x))}
68
- end
69
- #
70
- # Create a Proc that's curriable.
71
- # When curried, parameters are passed in from left to right.
72
- # i.e. curry(closure).call(a).call(b) is quivalent to closure.call(a,b) .
73
- # _block_ is encapsulated under the hood to perform the actual
74
- # job when currying is done.
75
- # arity explicitly specifies the number of parameters to curry.
76
- #
77
- def curry(arity, &block)
78
- fail "cannot curry for unknown arity" if arity < 0
79
- Functors.make_curry(arity, &block)
80
- end
81
- #
82
- # Create a Proc that's curriable.
83
- # When curried, parameters are passed in from right to left.
84
- # i.e. reverse_curry(closure).call(a).call(b) is quivalent to closure.call(b,a) .
85
- # _block_ is encapsulated under the hood to perform the actual
86
- # job when currying is done.
87
- # arity explicitly specifies the number of parameters to curry.
88
- #
89
- def reverse_curry(arity, &block)
90
- fail "cannot curry for unknown arity" if arity < 0
91
- Functors.make_reverse_curry(arity, &block)
92
- end
93
- #
94
- # Uncurry a curried closure.
95
- #
96
- def uncurry(&block)
97
- return block unless block.arity == 1
98
- proc do |*args|
99
- result = block
100
- args.each do |a|
101
- result = result.call(a)
102
- end
103
- result
104
- end
105
- end
106
- #
107
- # Uncurry a reverse curried closure.
108
- #
109
- def reverse_uncurry(&block)
110
- return block unless block.arity == 1
111
- proc do |*args|
112
- result = block
113
- args.reverse_each do |a|
114
- result = result.call(a)
115
- end
116
- result
117
- end
118
- end
119
- #
120
- # Create a Proc, when called,
121
- # repeatedly call _block_ for _n_ times.
122
- # The same arguments are passed to each invocation.
123
- #
124
- def repeat(n, &block)
125
- proc do |*args|
126
- result = nil
127
- n.times {result = block.call(*args)}
128
- result
129
- end
130
- end
131
- #
132
- # Create a Proc, when called,
133
- # repeatedly call _block_ for _n_ times.
134
- # At each iteration, return value from the previous iteration
135
- # is used as parameter.
136
- #
137
- def power(n, &block)
138
- return const(nil) if n<=0
139
- return block if n==1
140
- proc do |*args|
141
- result = block.call(*args)
142
- (n-1).times {result = block.call(result)}
143
- result
144
- end
145
- end
146
- extend self
147
- private_class_method
148
- def self.make_curry(arity, &block)
149
- return block if arity<=1
150
- proc do |x|
151
- make_curry(arity-1) do |*rest|
152
- block.call(*rest.insert(0, x))
153
- end
154
- end
155
- end
156
- def self.make_reverse_curry(arity, &block)
157
- return block if arity <= 1
158
- proc do |x|
159
- make_reverse_curry(arity-1) do |*rest|
160
- block.call(*rest << x)
161
- end
162
- end
163
- end
164
-
165
-
166
- end
167
-
168
- #
169
- # This module provides instance methods that
170
- # manipulate closures in a functional style.
171
- # It is typically included in Proc and Method.
172
- #
173
- module FunctorMixin
174
- #
175
- # Create a Proc, which expects the two parameters
176
- # in the reverse order of _self_.
177
- #
178
- def flip
179
- Functors.flip(&self)
180
- end
181
- #
182
- # Create a Proc, when called, the parameter is
183
- # first passed into _other_, _self_ is called in turn
184
- # with the return value from _other_.
185
- #
186
- def compose(other)
187
- Functors.compose(self, other)
188
- end
189
- alias << compose
190
- #
191
- # a >> b is equivalent to b << a
192
- #
193
- def >> (other)
194
- other << self
195
- end
196
- #
197
- # Create a Proc that's curriable.
198
- # When curried, parameters are passed in from left to right.
199
- # i.e. closure.curry.call(a).call(b) is quivalent to closure.call(a,b) .
200
- # _self_ is encapsulated under the hood to perform the actual
201
- # job when currying is done.
202
- # _ary_ explicitly specifies the number of parameters to curry.
203
- #
204
- def curry(ary=arity)
205
- Functors.curry(ary, &self)
206
- end
207
- #
208
- # Create a Proc that's curriable.
209
- # When curried, parameters are passed in from right to left.
210
- # i.e. closure.reverse_curry.call(a).call(b) is quivalent to closure.call(b,a) .
211
- # _self_ is encapsulated under the hood to perform the actual
212
- # job when currying is done.
213
- # _ary_ explicitly specifies the number of parameters to curry.
214
- #
215
- def reverse_curry(ary=arity)
216
- Functors.reverse_curry(ary, &self)
217
- end
218
- #
219
- # Uncurry a curried closure.
220
- #
221
- def uncurry
222
- Functors.uncurry(&self)
223
- end
224
- #
225
- # Uncurry a reverse curried closure.
226
- #
227
- def reverse_uncurry
228
- Functors.reverse_uncurry(&self)
229
- end
230
- #
231
- # Create a Proc, when called,
232
- # repeatedly call _self_ for _n_ times.
233
- # The same arguments are passed to each invocation.
234
- #
235
- def repeat(n)
236
- Functors.repeat(n, &self)
237
- end
238
- #
239
- # Create a Proc, when called,
240
- # repeatedly call _self_ for _n_ times.
241
- # At each iteration, return value from the previous iteration
242
- # is used as parameter.
243
- #
244
- def power(n)
245
- Functors.power(n, &self)
246
- end
247
- alias ** power
248
- alias * repeat
249
- end
1
+ #
2
+ # This module provides frequently used functors.
3
+ #
4
+ module Functors
5
+ Id = proc {|x|x}
6
+ Idn = proc {|*x|x}
7
+ Neg = proc {|x|-x}
8
+ Inc = proc {|x|x+1}
9
+ Dec = proc {|x|x-1}
10
+ Plus = proc {|x,y|x+y}
11
+ Minus = proc {|x,y|x-y}
12
+ Mul = proc {|x,y|x*y}
13
+ Div = proc {|x,y|x/y}
14
+ Mod = proc {|x,y|x%y}
15
+ Power = proc {|x,y|x**y}
16
+ Not = proc {|x,y|!x}
17
+ And = proc {|x,y|x&&y}
18
+ Or = proc {|x,y|x||y}
19
+ Xor = proc {|x,y|x^y}
20
+ BitAnd = proc {|x,y|x&y}
21
+ Union = proc {|x,y|x|y}
22
+ Match = proc {|x,y|x=~y}
23
+ Eq = proc {|x,y|x==y}
24
+ Ne = proc {|x,y|x!=y}
25
+ Lt = proc {|x,y|x<y}
26
+ Gt = proc {|x,y|x>y}
27
+ Le = proc {|x,y|x<=y}
28
+ Ge = proc {|x,y|x>=y}
29
+ Compare = proc {|x,y|x<=>y}
30
+ Call = proc {|x,y|x.call(y)}
31
+ Feed = proc {|x,y|y.call(x)}
32
+ Fst = proc {|x,_|x}
33
+ Snd = proc {|_, x|x}
34
+ At = proc {|x,y|x[y]}
35
+ To_a = proc {|x|x.to_a}
36
+ To_s = proc {|x|x.to_s}
37
+ To_i = proc {|x|x.to_i}
38
+ To_sym = proc {|x|x.to_sym}
39
+ To_f = proc {|x|x.to_f}
40
+
41
+ #
42
+ # Get a Proc, when called, always return the given value.
43
+ #
44
+ def const(v)
45
+ proc {|_|v}
46
+ end
47
+
48
+ #
49
+ # Get a Proc, when called, return the nth parameter.
50
+ #
51
+ def nth(n)
52
+ proc {|*args|args[n]}
53
+ end
54
+
55
+ #
56
+ # Create a Proc, which expects the two parameters
57
+ # in the reverse order of _block_.
58
+ #
59
+ def flip(&block)
60
+ proc {|x,y|block.call(y,x)}
61
+ end
62
+
63
+ #
64
+ # Create a Proc, when called, the parameter is
65
+ # first passed into _f2_, _f1_ is called in turn
66
+ # with the return value from _other_.
67
+ #
68
+ def compose(f1, f2)
69
+ proc {|*x|f1.call(f2.call(*x))}
70
+ end
71
+
72
+ #
73
+ # Create a Proc that's curriable.
74
+ # When curried, parameters are passed in from left to right.
75
+ # i.e. curry(closure).call(a).call(b) is quivalent to closure.call(a,b) .
76
+ # _block_ is encapsulated under the hood to perform the actual
77
+ # job when currying is done.
78
+ # arity explicitly specifies the number of parameters to curry.
79
+ #
80
+ def curry(arity, &block)
81
+ fail "cannot curry for unknown arity" if arity < 0
82
+ Functors.make_curry(arity, &block)
83
+ end
84
+
85
+ #
86
+ # Create a Proc that's curriable.
87
+ # When curried, parameters are passed in from right to left.
88
+ # i.e. reverse_curry(closure).call(a).call(b) is quivalent to closure.call(b,a) .
89
+ # _block_ is encapsulated under the hood to perform the actual
90
+ # job when currying is done.
91
+ # arity explicitly specifies the number of parameters to curry.
92
+ #
93
+ def reverse_curry(arity, &block)
94
+ fail "cannot curry for unknown arity" if arity < 0
95
+ Functors.make_reverse_curry(arity, &block)
96
+ end
97
+
98
+ #
99
+ # Uncurry a curried closure.
100
+ #
101
+ def uncurry(&block)
102
+ return block unless block.arity == 1
103
+ proc do |*args|
104
+ result = block
105
+ args.each do |a|
106
+ result = result.call(a)
107
+ end
108
+ result
109
+ end
110
+ end
111
+
112
+ #
113
+ # Uncurry a reverse curried closure.
114
+ #
115
+ def reverse_uncurry(&block)
116
+ return block unless block.arity == 1
117
+ proc do |*args|
118
+ result = block
119
+ args.reverse_each do |a|
120
+ result = result.call(a)
121
+ end
122
+ result
123
+ end
124
+ end
125
+
126
+ #
127
+ # Create a Proc, when called,
128
+ # repeatedly call _block_ for _n_ times.
129
+ # The same arguments are passed to each invocation.
130
+ #
131
+ def repeat(n, &block)
132
+ proc do |*args|
133
+ result = nil
134
+ n.times {result = block.call(*args)}
135
+ result
136
+ end
137
+ end
138
+
139
+ #
140
+ # Create a Proc, when called,
141
+ # repeatedly call _block_ for _n_ times.
142
+ # At each iteration, return value from the previous iteration
143
+ # is used as parameter.
144
+ #
145
+ def power(n, &block)
146
+ return const(nil) if n<=0
147
+ return block if n==1
148
+ proc do |*args|
149
+ result = block.call(*args)
150
+ (n-1).times {result = block.call(result)}
151
+ result
152
+ end
153
+ end
154
+
155
+ extend self
156
+
157
+ private_class_method
158
+
159
+ def self.make_curry(arity, &block)
160
+ return block if arity<=1
161
+ proc do |x|
162
+ make_curry(arity-1) do |*rest|
163
+ block.call(*rest.insert(0, x))
164
+ end
165
+ end
166
+ end
167
+
168
+ def self.make_reverse_curry(arity, &block)
169
+ return block if arity <= 1
170
+ proc do |x|
171
+ make_reverse_curry(arity-1) do |*rest|
172
+ block.call(*rest << x)
173
+ end
174
+ end
175
+ end
176
+
177
+ end
178
+
179
+ #
180
+ # This module provides instance methods that
181
+ # manipulate closures in a functional style.
182
+ # It is typically included in Proc and Method.
183
+ #
184
+ module FunctorMixin
185
+ #
186
+ # Create a Proc, which expects the two parameters
187
+ # in the reverse order of _self_.
188
+ #
189
+ def flip
190
+ Functors.flip(&self)
191
+ end
192
+
193
+ #
194
+ # Create a Proc, when called, the parameter is
195
+ # first passed into _other_, _self_ is called in turn
196
+ # with the return value from _other_.
197
+ #
198
+ def compose(other)
199
+ Functors.compose(self, other)
200
+ end
201
+
202
+ alias << compose
203
+
204
+ #
205
+ # a >> b is equivalent to b << a
206
+ #
207
+ def >> (other)
208
+ other << self
209
+ end
210
+
211
+ #
212
+ # Create a Proc that's curriable.
213
+ # When curried, parameters are passed in from left to right.
214
+ # i.e. closure.curry.call(a).call(b) is quivalent to closure.call(a,b) .
215
+ # _self_ is encapsulated under the hood to perform the actual
216
+ # job when currying is done.
217
+ # _ary_ explicitly specifies the number of parameters to curry.
218
+ #
219
+ def curry(ary=arity)
220
+ Functors.curry(ary, &self)
221
+ end
222
+
223
+ #
224
+ # Create a Proc that's curriable.
225
+ # When curried, parameters are passed in from right to left.
226
+ # i.e. closure.reverse_curry.call(a).call(b) is quivalent to closure.call(b,a) .
227
+ # _self_ is encapsulated under the hood to perform the actual
228
+ # job when currying is done.
229
+ # _ary_ explicitly specifies the number of parameters to curry.
230
+ #
231
+ def reverse_curry(ary=arity)
232
+ Functors.reverse_curry(ary, &self)
233
+ end
234
+
235
+ #
236
+ # Uncurry a curried closure.
237
+ #
238
+ def uncurry
239
+ Functors.uncurry(&self)
240
+ end
241
+
242
+ #
243
+ # Uncurry a reverse curried closure.
244
+ #
245
+ def reverse_uncurry
246
+ Functors.reverse_uncurry(&self)
247
+ end
248
+
249
+ #
250
+ # Create a Proc, when called,
251
+ # repeatedly call _self_ for _n_ times.
252
+ # The same arguments are passed to each invocation.
253
+ #
254
+ def repeat(n)
255
+ Functors.repeat(n, &self)
256
+ end
257
+ #
258
+
259
+ # Create a Proc, when called,
260
+ # repeatedly call _self_ for _n_ times.
261
+ # At each iteration, return value from the previous iteration
262
+ # is used as parameter.
263
+ #
264
+ def power(n)
265
+ Functors.power(n, &self)
266
+ end
267
+
268
+ alias ** power
269
+ alias * repeat
270
+ end