-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
metaops.pm
684 lines (621 loc) Β· 21.3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
sub METAOP_ASSIGN(\op) {
-> Mu \a, Mu \b { a = op.( ( a.DEFINITE ?? a !! op.() ), b) }
}
sub METAOP_TEST_ASSIGN:<//>(\lhs, $rhs) is raw { lhs // (lhs = $rhs()) }
sub METAOP_TEST_ASSIGN:<||>(\lhs, $rhs) is raw { lhs || (lhs = $rhs()) }
sub METAOP_TEST_ASSIGN:<&&>(\lhs, $rhs) is raw { lhs && (lhs = $rhs()) }
sub METAOP_TEST_ASSIGN:<andthen>(\lhs, $rhs) is raw { lhs andthen (lhs = $rhs()) }
sub METAOP_TEST_ASSIGN:<orelse>(\lhs, $rhs) is raw { lhs orelse (lhs = $rhs()) }
sub METAOP_NEGATE(\op) {
-> |c { c.elems > 1 ?? !op.(|c) !! True }
}
sub METAOP_REVERSE(\op) {
-> |args { op.(|args.reverse) }
}
sub METAOP_CROSS(\op, &reduce) {
return &infix:<X> if op === &infix:<,>;
-> +lol {
my $rop = lol.elems == 2 ?? op !! &reduce(op);
my $laze = False;
my @loi = eager for lol -> \elem {
if nqp::iscont(elem) {
$laze = False;
(elem,).iterator
}
else {
$laze = True if elem.is-lazy;
elem.iterator
}
}
my Mu $cache := nqp::list();
my int $i = 0;
for ^lol.elems {
$i = $_;
my Mu $rpa := nqp::list();
nqp::bindpos($cache, $i, $rpa);
}
my int $n = lol.elems - 1;
my $j = 0;
my @j;
my @v;
$i = 0;
gather {
while $i >= 0 {
my Mu $sublist := nqp::atpos($cache, $i);
if $j < nqp::elems($sublist) {
my Mu $o := nqp::atpos($sublist, $j);
@v[$i] := $o;
$j = $j + 1;
if $i >= $n { take lol.elems == 2 ?? $rop(|@v) !! $rop(@v); }
else { $i = $i + 1; @j.push($j); $j = 0; }
}
elsif !((my \value = @loi[$i].pull-one) =:= IterationEnd) {
nqp::bindpos($sublist, $j, value);
redo;
}
else {
$i = $i - 1;
if $i { $j = @j.pop if $i > 0 } # continue previous dimension where we left off
else {
$j = 0;
my Mu $sublist := nqp::atpos($cache,$i);
nqp::pop($sublist); # don't cache 1st dimension (could be infinite)
}
}
}
}.lazy-if($laze);
}
}
sub METAOP_ZIP(\op, &reduce) {
-> +lol {
my $arity = lol.elems;
my $rop = $arity == 2 ?? op !! &reduce(op);
my $laze = True;
my @loi = eager for lol -> \elem {
if nqp::iscont(elem) {
$laze = False;
Rakudo::Internals::WhateverIterator.new((elem,).iterator)
}
else {
$laze = False unless elem.is-lazy;
Rakudo::Internals::WhateverIterator.new(elem.iterator)
}
}
gather {
loop {
my \z = @loi.map: {
my \value = .pull-one;
last if value =:= IterationEnd;
value
};
my $z = List.from-iterator(z.iterator);
$z.eager;
last if $z.elems < $arity;
take-rw $arity == 2 ?? $rop(|$z) !! $rop(@$z);
}
}.lazy-if($laze);
}
}
proto sub METAOP_REDUCE_LEFT(|) { * }
multi sub METAOP_REDUCE_LEFT(\op, \triangle) {
if op.count > 2 and op.count < Inf {
my $count = op.count;
#?if jvm
my $ :=
#?endif
sub (+values) {
my \source = values.iterator;
my \first = source.pull-one;
return () if first =:= IterationEnd;
my @args.push: first;
GATHER({
take first;
until (my \current = source.pull-one) =:= IterationEnd {
@args.push: current;
if @args.elems == $count {
my \val = op.(|@args);
take val;
@args = ();
@args.push: val; # use of push allows op to return a Slip
}
}
}).lazy-if(source.is-lazy);
}
}
else {
#?if jvm
my $ :=
#?endif
sub (+values) {
my \source = values.iterator;
my \first = source.pull-one;
return () if first =:= IterationEnd;
my $result := first;
GATHER({
take first;
until (my \value = source.pull-one) =:= IterationEnd {
take ($result := op.($result, value));
}
}).lazy-if(source.is-lazy);
}
}
}
multi sub METAOP_REDUCE_LEFT(\op) {
if op.count > 2 and op.count < Inf {
my $count = op.count;
#?if jvm
my $ :=
#?endif
sub (+values) {
my \iter = values.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;
my @args.push: first;
my $result := first;
until (my \value = iter.pull-one) =:= IterationEnd {
@args.push: value;
if @args.elems == $count {
my \val = op.(|@args);
@args = ();
@args.push: val; # use of push allows op to return a Slip
$result := val;
}
}
$result;
}
}
else {
#?if jvm
my $ :=
#?endif
op =:= &infix:<+>
?? &sum
!! sub (+values) {
my \iter = values.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;
my \second = iter.pull-one;
return op.count <= 1 ?? op.(first) !! first if second =:= IterationEnd;
my $result := op.(first, second);
until (my \value = iter.pull-one) =:= IterationEnd {
$result := op.($result, value);
}
$result;
}
}
}
proto sub METAOP_REDUCE_RIGHT(|) { * }
multi sub METAOP_REDUCE_RIGHT(\op, \triangle) {
if op.count > 2 and op.count < Inf {
my $count = op.count;
#?if jvm
my $ :=
#?endif
sub (+values) {
my \source = values.reverse.iterator;
my \first = source.pull-one;
return () if first =:= IterationEnd;
my @args.unshift: first;
GATHER({
take first;
while !((my \current = source.pull-one) =:= IterationEnd) {
@args.unshift: current;
if @args.elems == $count {
my \val = op.(|@args);
take val;
@args = ();
@args.unshift: val; # allow op to return a Slip
}
}
}).lazy-if(source.is-lazy);
}
}
else {
sub (+values) {
my \iter = values.reverse.iterator;
my $result := iter.pull-one;
return () if $result =:= IterationEnd;
gather {
take $result;
while !((my $elem := iter.pull-one) =:= IterationEnd) {
take $result := op.($elem, $result)
}
}.lazy-if(values.is-lazy);
}
}
}
multi sub METAOP_REDUCE_RIGHT(\op) {
if op.count > 2 and op.count < Inf {
my $count = op.count;
#?if jvm
my $ :=
#?endif
sub (+values) {
my \iter = values.reverse.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;
my @args.unshift: first;
my $result := first;
until (my \value = iter.pull-one) =:= IterationEnd {
@args.unshift: value;
if @args.elems == $count {
my \val = op.(|@args);
@args = ();
@args.unshift: val; # allow op to return a Slip
$result := val;
}
}
$result;
}
}
else {
#?if jvm
my $ :=
#?endif
sub (+values) {
my \iter = values.reverse.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;
my \second = iter.pull-one;
return op.(first) if second =:= IterationEnd;
my $result := op.(second, first);
until (my \value = iter.pull-one) =:= IterationEnd {
$result := op.(value, $result);
}
$result;
}
}
}
proto sub METAOP_REDUCE_LIST(|) { * }
multi sub METAOP_REDUCE_LIST(\op, \triangle) {
#?if jvm
my $ :=
#?endif
sub (+values) {
GATHER({
my @list;
for values -> \v {
@list.append(v);
take op.(|@list);
}
}).lazy-if(values.is-lazy);
}
}
multi sub METAOP_REDUCE_LIST(\op) {
#?if jvm
my $ :=
#?endif
sub (+values) { op.(|values) }
}
proto sub METAOP_REDUCE_LISTINFIX(|) { * }
multi sub METAOP_REDUCE_LISTINFIX(\op, \triangle) {
#?if jvm
my $ :=
#?endif
sub (|values) {
my \p = values[0];
return () unless p.elems;
my int $i;
GATHER({
my @list;
while $i < p.elems {
@list.append(p[$i]);
$i = $i + 1;
take op.(|@list);
}
}).lazy-if(p.is-lazy);
}
}
multi sub METAOP_REDUCE_LISTINFIX(\op) {
#?if jvm
my $ :=
#?endif
sub (+values) {
op.(|values.map({nqp::decont($_)}));
}
}
proto sub METAOP_REDUCE_CHAIN(|) { * }
multi sub METAOP_REDUCE_CHAIN(\op, \triangle) {
#?if jvm
my $ :=
#?endif
sub (+values) {
my $state = True;
my \iter = values.iterator;
my Mu $current = iter.pull-one;
gather {
take $state;
while $state && !((my $next := iter.pull-one) =:= IterationEnd) {
$state = op.($current, $next);
take $state;
$current := $next;
}
unless $state {
while !((my \v = iter.pull-one) =:= IterationEnd) {
take False;
}
}
}.lazy-if(values.is-lazy);
}
}
multi sub METAOP_REDUCE_CHAIN(\op) {
#?if jvm
my $ :=
#?endif
sub (+values) {
my $state := True;
my \iter = values.iterator;
my $current := iter.pull-one;
return True if $current =:= IterationEnd;
while !((my $next := iter.pull-one) =:= IterationEnd) {
$state := op.($current, $next);
return $state unless $state;
$current := $next;
}
$state;
}
}
sub METAOP_REDUCE_XOR(\op, $triangle?) {
X::NYI.new(feature => 'xor reduce').throw;
}
sub METAOP_HYPER(\op, *%opt) {
-> Mu \a, Mu \b { HYPER(op, a, b, |%opt) }
}
proto sub METAOP_HYPER_POSTFIX(|) {*}
multi sub METAOP_HYPER_POSTFIX(\op) {
op.?nodal
?? (-> \obj { nodemap(op, obj) })
!! (-> \obj { deepmap(op, obj) })
}
# no indirection for subscripts and such
proto sub METAOP_HYPER_POSTFIX_ARGS(|) {*}
multi sub METAOP_HYPER_POSTFIX_ARGS(\obj,\op) {
op.?nodal
?? nodemap(op, obj)
!! deepmap(op, obj)
}
multi sub METAOP_HYPER_POSTFIX_ARGS(\obj, @args, \op) {
op.?nodal
?? nodemap( -> \o { op.(o,@args) }, obj )
!! deepmap( -> \o { op.(o,@args) }, obj );
}
multi sub METAOP_HYPER_POSTFIX_ARGS(\obj, \args, \op) {
op.?nodal
?? nodemap( -> \o { op.(o,|args) }, obj )
!! deepmap( -> \o { op.(o,|args) }, obj );
}
sub METAOP_HYPER_PREFIX(\op) {
op.?nodal # rarely true for prefixes
?? (-> \obj { nodemap(op, obj) })
!! (-> \obj { deepmap(op, obj) })
}
sub METAOP_HYPER_CALL(\list, |args) { deepmap(-> $c { $c(|args) }, list) }
proto sub HYPER(|) { * }
multi sub HYPER(&op, \left, \right, :$dwim-left, :$dwim-right) {
op(left, right);
}
multi sub HYPER(&op, Associative:D \left, Associative:D \right, :$dwim-left, :$dwim-right) {
my %keyset;
if !$dwim-left {
%keyset{$_} = 1 for left.keys;
}
else {
%keyset{$_} = 1 if right.EXISTS-KEY($_) for left.keys;
}
if !$dwim-right {
%keyset{$_} = 1 for right.keys;
}
my @keys = %keyset.keys;
my $type = left.WHAT;
my \result := $type.new;
result = @keys Z=> HYPER(&op, left{@keys}, right{@keys}, :$dwim-left, :$dwim-right);
nqp::iscont(left) ?? result.item !! result;
}
multi sub HYPER(&op, Associative:D \left, \right, :$dwim-left, :$dwim-right) {
my @keys = left.keys;
my $type = left.WHAT;
my \result := $type.new;
result = @keys Z=> HYPER(&op, left{@keys}, right, :$dwim-left, :$dwim-right);
nqp::iscont(left) ?? result.item !! result;
}
multi sub HYPER(&op, \left, Associative:D \right, :$dwim-left, :$dwim-right) {
my @keys = right.keys;
my $type = right.WHAT;
my \result := $type.new;
result = @keys Z=> HYPER(&op, left, right{@keys}, :$dwim-left, :$dwim-right);
nqp::iscont(right) ?? result.item !! result;
}
multi sub HYPER(&operator, Positional:D \left, \right, :$dwim-left, :$dwim-right) {
my @result;
X::HyperOp::Infinite.new(:side<left>, :&operator).throw if left.is-lazy;
my int $elems = left.elems;
X::HyperOp::NonDWIM.new(:&operator, :left-elems($elems), :right-elems(1), :recursing(callframe(3).code.name eq 'HYPER')).throw
unless $elems == 1 or $elems > 1 and $dwim-right or $elems == 0 and $dwim-left || $dwim-right;
my \lefti := left.iterator;
my int $i = 0;
until (my \value := lefti.pull-one) =:= IterationEnd {
@result[$i++] := HYPER(&operator, value, right, :$dwim-left, :$dwim-right);
}
# Coerce to the original type if it's a subtype of List
my $type = nqp::istype(left, List) ?? left.WHAT !! List;
nqp::iscont(left) ?? $type(|@result.eager).item !! $type(|@result.eager)
}
multi sub HYPER(&operator, \left, Positional:D \right, :$dwim-left, :$dwim-right) {
my @result;
X::HyperOp::Infinite.new(:side<right>, :&operator).throw if right.is-lazy;
my int $elems = right.elems;
X::HyperOp::NonDWIM.new(:&operator, :left-elems(1), :right-elems($elems), :recursing(callframe(3).code.name eq 'HYPER')).throw
unless $elems == 1 or $elems > 1 and $dwim-left or $elems == 0 and $dwim-left || $dwim-right;
my \righti := right.iterator;
my int $i = 0;
until (my \value := righti.pull-one) =:= IterationEnd {
@result[$i++] := HYPER(&operator, left, value, :$dwim-left, :$dwim-right);
}
# Coerce to the original type if it's a subtype of List
my $type = nqp::istype(right, List) ?? right.WHAT !! List;
nqp::iscont(right) ?? $type(|@result.eager).item !! $type(|@result.eager)
}
multi sub HYPER(&operator, Iterable:D \left, Iterable:D \right, :$dwim-left, :$dwim-right) {
my \left-iterator = left.iterator;
my \right-iterator = right.iterator;
# Check whether any side is lazy. They must not be to proceed.
if left-iterator.is-lazy {
X::HyperOp::Infinite.new(:side<both>, :&operator).throw if right-iterator.is-lazy;
X::HyperOp::Infinite.new(:side<left>, :&operator).throw if not $dwim-left or $dwim-right;
}
X::HyperOp::Infinite.new(:side<right>, :&operator).throw if right-iterator.is-lazy and
(not $dwim-right or $dwim-left);
my \lefti := Rakudo::Internals::DwimIterator.new(left-iterator);
my \righti := Rakudo::Internals::DwimIterator.new(right-iterator);
my \result := IterationBuffer.new;
loop {
my \leftv := lefti.pull-one;
my \rightv := righti.pull-one;
X::HyperOp::NonDWIM.new(:&operator, :left-elems(lefti.count-elems), :right-elems(righti.count-elems), :recursing(callframe(3).code.name eq 'HYPER')).throw
if !$dwim-left and !$dwim-right and (lefti.ended != righti.ended);
last if ($dwim-left and $dwim-right) ?? (lefti.ended and righti.ended) !!
(($dwim-left or lefti.ended) and ($dwim-right or righti.ended));
last if $++ == 0 and ($dwim-left and lefti.ended or $dwim-right and righti.ended);
result.push(HYPER(&operator, leftv, rightv, :$dwim-left, :$dwim-right));
}
# Coerce to the original type
my $type = nqp::istype(left, List) ?? left.WHAT !! List; # keep subtypes of List
my \retval = $type.new;
nqp::bindattr(retval, List, '$!reified', result);
nqp::iscont(left) ?? retval.item !! retval;
}
multi sub HYPER(\op, \obj) {
op.?nodal
?? nodemap(op, obj)
!! deepmap(op,obj);
}
proto sub deepmap(|) { * }
multi sub deepmap(\op, \obj) {
#my Mu $rpa := nqp::list();
#my \objs := obj.list;
# as a wanted side-effect is-lazy reifies the list
#fail X::Cannot::Lazy.new(:action('deepmap')) if objs.is-lazy;
my \iterable = obj.DEFINITE && nqp::istype(obj, Iterable)
?? obj
!! obj.list;
my \result := class :: does SlippyIterator {
has &!block;
has $!source;
method new(&block, $source) {
my $iter := nqp::create(self);
nqp::bindattr($iter, self, '&!block', &block);
nqp::bindattr($iter, self, '$!source', $source);
$iter
}
method is-lazy() {
$!source.is-lazy
}
method pull-one() is raw {
my int $redo = 1;
my $value;
my $result;
if $!slipping && !(($result := self.slip-one()) =:= IterationEnd) {
$result
}
elsif ($value := $!source.pull-one()) =:= IterationEnd {
$value
}
else {
nqp::while(
$redo,
nqp::stmts(
$redo = 0,
nqp::handle(
nqp::stmts(
nqp::if(
nqp::istype($value, Iterable),
nqp::stmts(
($result := deepmap(&!block, $value).item),
),
($result := &!block($value)),
),
nqp::if(
nqp::istype($result, Slip),
nqp::stmts(
($result := self.start-slip($result)),
nqp::if(
nqp::eqaddr($result, IterationEnd),
nqp::stmts(
($value := $!source.pull-one()),
($redo = 1 unless nqp::eqaddr($value, IterationEnd))
))
))
),
'NEXT', nqp::stmts(
($value := $!source.pull-one()),
nqp::eqaddr($value, IterationEnd)
?? ($result := IterationEnd)
!! ($redo = 1)),
'REDO', $redo = 1,
'LAST', ($result := IterationEnd))),
:nohandler);
$result
}
}
}.new(op, iterable.iterator);
my $type = nqp::istype(obj, List) ?? obj.WHAT !! List; # keep subtypes of List
my \buffer := IterationBuffer.new;
result.push-all(buffer);
my \retval = $type.new;
nqp::bindattr(retval, List, '$!reified', buffer);
nqp::iscont(obj) ?? retval.item !! retval;
}
multi sub deepmap(\op, Associative \h) {
my @keys = h.keys;
hash @keys Z deepmap(op, h{@keys})
}
proto sub nodemap(|) { * }
multi sub nodemap(\op, \obj) {
my Mu $rpa := nqp::list();
my \objs := obj.list;
# as a wanted side-effect is-lazy reifies the list
fail X::Cannot::Lazy.new(:action('deepmap')) if objs.is-lazy;
my Mu $items := nqp::getattr(objs, List, '$!reified');
my Mu $o;
# We process the elements in two passes, end to start, to
# prevent users from relying on a sequential ordering of hyper.
# Also, starting at the end pre-allocates $rpa for us.
my int $i = nqp::elems($items) - 1;
nqp::while(
nqp::isge_i($i, 0),
nqp::stmts(
($o := nqp::atpos($items, $i)),
nqp::bindpos($rpa, $i,
nqp::if(Mu, # hack cuz I don't understand nqp
$o.new(nodemap(op, $o)).item,
op.($o))),
$i = nqp::sub_i($i, 2)
)
);
$i = nqp::elems($items) - 2;
nqp::while(
nqp::isge_i($i, 0),
nqp::stmts(
($o := nqp::atpos($items, $i)),
nqp::bindpos($rpa, $i,
nqp::if(Mu, # hack cuz I don't understand nqp
$o.new(nodemap(op, $o)).item,
op.($o))),
$i = nqp::sub_i($i, 2)
)
);
nqp::p6bindattrinvres(nqp::create(List), List, '$!reified', $rpa)
}
multi sub nodemap(\op, Associative \h) {
my @keys = h.keys;
hash @keys Z nodemap(op, h{@keys})
}
proto sub duckmap(|) { * }
multi sub duckmap(\op, \obj) {
nodemap(-> \arg { try { op.(arg) } // try { duckmap(op,arg) } }, obj);
}
multi sub duckmap(\op, Associative \h) {
my @keys = h.keys;
hash @keys Z duckmap(op, h{@keys})
}
# vim: ft=perl6 expandtab sw=4