/
metaops.pm
290 lines (259 loc) · 8.48 KB
/
metaops.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
our multi sub negate(&op, Mu \$a, Mu \$b) {
!(&op($a, $b));
}
our multi sub negate(&op) {
Bool::True;
}
our multi sub reverseargs(&op, Mu \$a, Mu \$b) {
&op($b, $a);
}
our multi sub sequentialargs(&op, Mu \$a, Mu \$b) {
# CHEAT: this needs to do something more once Rakudo gets threading
&op($a, $b);
}
our multi sub zipwith(&op, Iterable $a-iterable, Iterable $b-iterable) {
my $ai = $a-iterable.iterator;
my $bi = $b-iterable.iterator;
gather loop {
my $a = $ai.get;
my $b = $bi.get;
last if ($a ~~ EMPTY) || ($b ~~ EMPTY);
take &op($a, $b);
}
}
our multi sub zipwith(&op, $a, $b) {
zipwith(&op, $a.list, $b.list);
}
our multi sub crosswith(&op, Iterable $a-iterable, Iterable $b-iterable) {
my $ai = $a-iterable.iterator;
my @b = $b-iterable.Seq;
gather loop {
my $a = $ai.get;
last if ($a ~~ EMPTY);
for @b -> $b {
take &op($a, $b);
}
}
}
our multi sub crosswith(&op, $a, $b) {
crosswith(&op, $a.list, $b.list);
}
our multi reduce(&op, $list) {
$list.reduce(&op)
}
our multi reduce(&op, *@list) {
@list.reduce(&op)
}
our multi sub hyper(&op, Iterable $lhs-iterable, Iterable $rhs-iterable, :$dwim-left, :$dwim-right) {
my sub repeating-array(@a) {
gather loop {
for @a -> $a {
take $a;
}
}
}
my @lhs = $lhs-iterable.Seq;
my @rhs = $rhs-iterable.Seq;
my $length;
if !$dwim-left && !$dwim-right {
if +@lhs != +@rhs {
die "Sorry, sides are of uneven length and not dwimmy.";
}
$length = +@lhs;
} elsif !$dwim-left {
$length = +@lhs;
} elsif !$dwim-right {
$length = +@rhs;
} else {
$length = +@lhs max +@rhs;
}
if $length != +@lhs {
@lhs = repeating-array(@lhs).batch($length);
}
if $length != +@rhs {
@rhs = repeating-array(@rhs).batch($length);
}
my @result;
for @lhs Z @rhs -> $l, $r {
if Associative.ACCEPTS($l) || Associative.ACCEPTS($r) {
@result.push(hyper(&op, $l, $r, :$dwim-left, :$dwim-right));
} elsif Iterable.ACCEPTS($l) || Iterable.ACCEPTS($r) {
@result.push([hyper(&op, $l.list, $r.list, :$dwim-left, :$dwim-right)]);
} else {
@result.push(op($l, $r));
}
}
@result
}
our multi sub hyper(&op, $lhs, $rhs, :$dwim-left, :$dwim-right) {
hyper(&op, $lhs.list, $rhs.list, :$dwim-left, :$dwim-right);
}
our multi sub hyper(&op, %lhs, %rhs, :$dwim-left, :$dwim-right) {
my %result;
my @keys;
if $dwim-left && $dwim-right {
@keys = %lhs.keys.grep({ %rhs.exists($_) });
} elsif $dwim-left {
@keys = %rhs.keys;
} elsif $dwim-right {
@keys = %lhs.keys;
} else {
# .eagers should not be necessary in next line, but are
# needed ATM because of the gather / take bug.
@keys = (%lhs.keys.eager, %rhs.keys.eager).uniq;
}
for @keys -> $key {
%result{$key} = &op(%lhs{$key}, %rhs{$key});
}
%result;
}
our multi sub hyper(&op, %arg) {
my %result;
for %arg.keys -> $key {
%result{$key} = &op(%arg{$key});
}
%result;
}
our multi sub hyper(&op, %lhs, $rhs, :$dwim-left, :$dwim-right) {
die "Sorry, right side is too short and not dwimmy." unless $dwim-right;
my %result;
for %lhs.keys -> $key {
%result{$key} = &op(%lhs{$key}, $rhs);
}
%result;
}
our multi sub hyper(&op, $lhs, %rhs, :$dwim-left, :$dwim-right) {
die "Sorry, left side is too short and not dwimmy." unless $dwim-left;
my %result;
for %rhs.keys -> $key {
%result{$key} = &op($lhs, %rhs{$key});
}
%result;
}
our multi sub hyper(&op, @arg) {
my @result;
for @arg {
# this is terribly ugly; but works
@result.push(hyper(&op, $_)) if Associative.ACCEPTS($_);
@result.push([hyper(&op, $_)]) if !Associative.ACCEPTS($_) && Iterable.ACCEPTS($_);
@result.push(op($_)) if !Associative.ACCEPTS($_) && !Iterable.ACCEPTS($_);
}
@result
}
our multi sub hyper(&op, $arg) {
hyper(&op, $arg.list)
}
our multi sub reducewith(&op, Iterable $an-iterable,
:$chaining,
:$right-assoc,
:$triangle) {
my $ai = $an-iterable.iterator;
$ai = $ai.Seq.reverse.iterator if $right-assoc;
if $triangle {
gather {
my $result = $ai.get;
return if $result ~~ EMPTY;
if $chaining {
my $bool = Bool::True;
take Bool::True;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
$bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
my $temp = $bool;
take $temp;
$result = $next;
}
} else {
my $temp = $result;
take $temp;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
$result = $right-assoc ?? &op($next, $result) !! &op($result, $next);
my $temp = $result;
take $temp;
}
}
}
} else {
my $result = $ai.get;
if $result ~~ EMPTY {
return &op();
}
if $chaining {
my $bool = Bool::True;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
$bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
$result = $next;
}
return $bool;
} else {
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
$result = $right-assoc ?? &op($next, $result) !! &op($result, $next);
}
}
$result;
}
}
our multi sub reducewith(&op, $arg,
:$chaining,
:$right-assoc,
:$triangle) {
reducewith(&op, $arg.list, :$chaining, :$right-assoc, :$triangle);
}
# degenerate case of operators, to be used by reduce() for the 0-ary case
# this fails for operators defined in PIR, so some of them are commented out.
our multi sub infix:<**>() { 1 }
our multi sub infix:<*>() { 1 }
our multi sub infix:<+&>() { +^0 }
our multi sub infix:<+>() { 0 }
our multi sub infix:<->() { 0 }
our multi sub infix:<~>() { '' }
our multi sub infix:<+|>() { 0 }
our multi sub infix:<+^>() { 0 }
our multi sub infix:<~|>() { '' }
our multi sub infix:<~^>() { '' }
#our multi sub infix:<&>() { all() }
#our multi sub infix:<|>() { any() }
#our multi sub infix:<^>() { one() }
our multi sub infix:<==>() { Bool::True }
our multi sub infix:<!=>() { Bool::True }
our multi sub infix:«<»() { Bool::True }
our multi sub infix:«<=»() { Bool::True }
our multi sub infix:«>»() { Bool::True }
our multi sub infix:«>=»() { Bool::True }
our multi sub infix:<before>() { Bool::True }
our multi sub infix:<after>() { Bool::True }
our multi sub infix:<~~>() { Bool::True }
our multi sub infix:<lt>() { Bool::True }
our multi sub infix:<le>() { Bool::True }
our multi sub infix:<gt>() { Bool::True }
our multi sub infix:<ge>() { Bool::True }
our multi sub infix:<eq>() { Bool::True }
our multi sub infix:<ne>() { Bool::True }
#our multi sub infix:<===>() { Bool::True }
our multi sub infix:<eqv>() { Bool::True }
#
our multi sub infix:<||>() { Bool::False }
our multi sub infix:<or>() { Bool::False }
#our multi sub infix:<^^>() { Bool::False }
our multi sub infix:<//>() { Any }
#our multi sub infix:<min>() { +Inf }
#our multi sub infix:<max>() { -Inf }
#our multi sub infix:<=>() { Nil }
#our multi sub infix:<:=>() { Nil }
#our multi sub infix:<,>() { [] }
our multi sub infix:<Z>() { [] }
our multi sub infix:</>() { fail "No zero-arg meaning for infix:</>"; }
our multi sub infix:<%>() { fail "No zero-arg meaning for infix:<%>"; }
our multi sub infix:<x>() { fail "No zero-arg meaning for infix:<x>"; }
our multi sub infix:<xx>() { fail "No zero-arg meaning for infix:<xx>"; }
our multi sub infix:«+<»() { fail "No zero-arg meaning for infix:«+<»"; }
our multi sub infix:«+>»() { fail "No zero-arg meaning for infix:«+>»"; }
our multi sub infix:<~&>() { fail "No zero-arg meaning for infix:<~&>"; }
# vim: ft=perl6