Skip to content

Commit

Permalink
add List.push, Seq to array prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 6, 2010
1 parent 2ceeaa4 commit 2ba29b2
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions test2.pl
@@ -1,18 +1,30 @@
# 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);
}
}

method push(*@x) {
$!rest.push(@x);
}

method Numeric { self.elems }
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)
});
}
PRE-INIT { Q:CgOp { (prog (rawsset Kernel.ArrayP (@ (l Array))) (null Variable)) } }
sub prefix:<+>($x) { $x.Numeric }

Expand All @@ -25,5 +37,15 @@ sub prefix:<+>($x) { $x.Numeric }
@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;

0 comments on commit 2ba29b2

Please sign in to comment.