/
operators.pm
272 lines (222 loc) · 5.54 KB
/
operators.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
our multi infix:<~~>(Mu $topic, Mu $matcher) {
$matcher.ACCEPTS($topic)
}
our multi infix:<~~>(Mu $topic, Regex $matcher) {
Q:PIR {
$P0 = find_lex '$matcher'
$P1 = find_lex '$topic'
%r = $P0.'ACCEPTS'($P1)
store_dynamic_lex '$/', %r
};
}
our multi infix:<!~~>(Mu $topic, Mu $matcher) {
$matcher.REJECTS($topic)
}
our multi prefix:<?>(Mu $a) {
$a.Bool;
}
our multi prefix:<!>(Mu $a) {
$a.Bool ?? False !! True;
}
our multi sub prefix:<->($a) {
pir::box__PN(pir::neg__NN($a))
}
our multi sub infix:<+>($a, $b) {
pir::box__PN(pir::add__NNN($a, $b))
}
our multi sub infix:<->($a, $b) {
pir::box__PN(pir::sub__NNN($a, $b))
}
our multi sub infix:<*>($a, $b) {
pir::box__PN(pir::mul__NNN($a, $b))
}
our multi sub infix:</>($a, $b) {
pir::box__PN(pir::div__NNN($a, $b))
}
our multi sub infix:<%>($a, $b) {
pir::box__PN(pir::mod__NNN($a, $b))
}
our multi sub infix:<**>($a, $b) {
pir::box__PN(pir::pow__NNN($a, $b))
}
our multi sub infix:<&>(*@items) {
Junction.new(@items, :all)
}
our multi sub infix:<|>(*@items) {
Junction.new(@items, :any)
}
our multi sub infix:<^>(*@items) {
Junction.new(@items, :one)
}
our sub all(*@items) {
Junction.new(@items, :all);
}
our sub any(*@items) {
Junction.new(@items, :any);
}
our sub one(*@items) {
Junction.new(@items, :one);
}
our sub none(*@items) {
Junction.new(@items, :none);
}
our multi prefix:<not>($x) { !$x }
our multi prefix:<so>($x) { ?$x }
our multi prefix:sym<+^>($x) {
pir::bnot__PP($x)
}
our sub undefine(Mu \$x) {
my $undefined;
$x = $undefined;
}
our multi infix:<does>(Mu \$do-it-to-me, Role $r) {
my $specific_role = $r!select;
my $applicator = $specific_role.^applier_for($do-it-to-me);
$applicator.apply($do-it-to-me, [$r]);
$do-it-to-me
}
our multi infix:<but>(Mu $do-it-to-me is copy, \$r) {
$do-it-to-me does $r
}
our multi infix:<before>($a, $b) {
($a cmp $b) == -1;
}
our multi infix:<after>($a, $b) {
($a cmp $b) == +1;
}
our multi infix:<?|>($a, $b) {
?(?$a +| ?$b)
}
our multi infix:<?&>($a, $b) {
?(?$a +& ?$b)
}
our multi infix:<?^>($a, $b) {
?(?$a +^ ?$b)
}
our multi infix:<min>(*@args) {
@args.min;
}
our multi infix:<max>(*@args) {
@args.max;
}
our multi infix:«=>»($key, Mu $value) {
Pair.new(key => $key, value => $value);
}
our multi infix:<~>($a, $b) {
my $result = pir::new__Ps('Str');
pir::assign__vPS($result, pir::concat__SSS(~$a, ~$b));
$result
}
our sub circumfix:<{ }>(*@elements) {
my %h = @elements;
%h
}
our sub hash(*@list, *%hash) {
my %h = (@list, %hash);
%h
}
our multi infix:sym<//>(Mu $a, Mu $b) {
$a.defined ?? $a !! $b
}
our multi infix:<==>($a, $b) {
pir::iseq__INN(+$a, +$b) ?? True !! False
}
our multi infix:<!=>($a, $b) {
pir::isne__INN(+$a, +$b) ?? True !! False
}
# XXX Lazy version would be nice in the future too.
our multi infix:<xx>(Mu \$item, $n) {
my @result = ();
for 1..$n {
@result.push($item);
}
@result
}
our multi prefix:<|>(@a) { @a.Capture }
our multi prefix:<|>(%h) { %h.Capture }
our multi prefix:<|>(Capture $c) { $c }
our multi prefix:<|>(Mu $fail) { die 'Cannot use prefix:<|> with a ' ~ $fail.WHAT; }
our multi infix:<:=>(Mu $a, Mu $b) {
die ":= binding of variables not yet implemented";
}
our multi infix:<:=>(Signature $s, Parcel \$p) {
$s!BIND($p.Capture());
}
our multi infix:<:=>(Signature $s, Mu \$val) {
$s!BIND(Capture.new($val));
}
# XXX Wants to be a macro when we have them.
our sub WHAT(\$x) {
$x.WHAT
}
class Whatever { ... }
# the magic one that handles stuff like
# 'a' ... 'z' and 'z' ... 'a'
our multi sub infix:<...>($lhs, $rhs) {
if $rhs ~~ Whatever {
my $i = $lhs;
return gather {
loop {
my $j = $i++;
take $j;
}
}
}
gather {
take $lhs;
if ($lhs cmp $rhs) == 1 {
my $x = $lhs;
# since my $a = 'a'; $a-- gives
# "Decrement out of range" we can't easily
# decrement over our target, which is why the
# case of going backwards is slighly more complicated
# than going forward
while (--$x cmp $rhs) == 1 {
# need to make a fresh copy here because of RT #62178
my $y = $x;
take $y;
}
take $x if ($x cmp $rhs) == 0;
} elsif ($lhs cmp $rhs) == -1 {
my $x = $lhs;
while (++$x cmp $rhs) <= 0 {
my $y = $x;
take $y;
}
}
}
}
our multi sub infix:<...>($lhs, Code $rhs) {
if $rhs.count != 1 {
die "Series operator currently cannot handle blocks with count != 1";
}
my $i = $lhs;
gather {
my $j = $i;
take $j;
my $last = $i;
loop {
$i = $rhs.($last);
my $j = $i;
take $j;
$last = $i;
}
}
}
our multi sub infix:<...>(@lhs, Whatever) {
given @lhs.elems {
when 2 {
@lhs[0] ... { $_ + (@lhs[1] - @lhs[0]) };
}
when 3 {
if @lhs[1] - @lhs[0] == @lhs[2] - @lhs[1] {
@lhs[0] ... { $_ + (@lhs[1] - @lhs[0]) };
} elsif @lhs[1] / @lhs[0] == @lhs[2] / @lhs[1] {
@lhs[0] ... { $_ * (@lhs[1] / @lhs[0]) };
} else {
fail "Unable to figure out pattern of series";
}
}
default { fail "Unable to figure out pattern of series"; }
}
}