Skip to content

Commit

Permalink
Add pop to arrays prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 6, 2010
1 parent 2ba29b2 commit 241de60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions SAFE.setting
Expand Up @@ -414,6 +414,11 @@ PRE-INIT {
my class LLArray {
method push is rawcall { Q:CgOp { (prog
[rawcall (unbox List<Variable> (@ (pos 0))) Add (pos 1)] [pos 0]) } }
method pop() { Q:CgOp { (withtypes $f Variable $lv List<Variable>
[l $lv (unbox List<Variable> (@ (l self)))]
[l $f (getindex (- (getfield Count (l $lv)) (int 1)) (l $lv))]
[rawcall (l $lv) RemoveAt (- (getfield Count (l $lv)) (int 1))]
[l $f]) } }
method shift() { Q:CgOp { (withtypes $f Variable $lv List<Variable>
[l $lv (unbox List<Variable> (@ (l self)))]
[l $f (getindex (int 0) (l $lv))]
Expand Down
9 changes: 9 additions & 0 deletions test2.pl
Expand Up @@ -24,6 +24,10 @@
List.HOW.add-method("push", anon method push(*@items) {
$!rest.push(@items.Seq.eager.iterator)
});
List.HOW.add-method("pop", anon method pop() {
self.eager;
$!items.pop;
});
}
sub prefix:<+>($x) { $x.Numeric }
Expand All @@ -48,4 +52,9 @@ sub prefix:<+>($x) { $x.Numeric }
$k = 3;
ok @x.shift == 2, "push copies";

@x.push(11,12);
ok @x.pop == 12, "pop is LIFO (1)";
ok @x.pop == 11, "pop is LIFO (2)";
ok +@x == 0, "pop removed all elements";

done-testing;

0 comments on commit 241de60

Please sign in to comment.