/
test2.pl
51 lines (41 loc) · 1.23 KB
/
test2.pl
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
# vim: ft=perl6
use Test;
# exactly like List, but flattens, and with "is copy" semantics on stuff
my class Seq is List {
method !elem($x) { my $y = $x; $y }
method Seq { self }
}
my class Array is List {
method new() {
Array.RAWCREATE("flat", 1, "items", LLArray.new, "rest", LLArray.new);
}
}
PRE-INIT {
Q:CgOp { (prog (rawsset Kernel.ArrayP (@ (l Array))) (null Variable)) };
List.HOW.add-method("Seq", anon method Seq() {
Seq.RAWCREATE("flat", 1, "items", LLArray.new,
"rest", LLArray.new(self.iterator));
});
List.HOW.add-method("Numeric", anon method Numeric () { self.elems });
List.HOW.add-method("push", anon method push(*@items) {
$!rest.push(@items.Seq.eager.iterator)
});
}
sub prefix:<+>($x) { $x.Numeric }
my @x;
ok @x ~~ Array, '@x isa Array';
ok @x.elems == 0, 'no elements';
ok +@x == 0, 'no elements (+)';
@x.push(5);
ok @x.elems == 1, 'one element now';
ok @x.shift == 5, 'element removed is 5';
@x.push(7,8);
ok @x.elems == 2, "added two elements";
ok @x.shift == 7, "removed first correctly";
ok @x.shift == 8, "removed second correctly";
ok @x.elems == 0, "no elements again";
my $k = 2;
@x.push($k);
$k = 3;
ok @x.shift == 2, "push copies";
done-testing;