-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
operators.pm
200 lines (163 loc) · 3.89 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
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 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 sub undefine(\$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, $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
}
# XXX Wants to be a macro when we have them.
our sub WHAT(\$x) {
$x.WHAT
}
# 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 {
take $i;
my $last = $i;
loop {
$i = $rhs.($last);
my $j = $i;
take $j;
$last = $i;
}
}
}