Skip to content

Commit

Permalink
Simple port of Array.delete from alpha.
Browse files Browse the repository at this point in the history
This implementation works but is not lazy.
  • Loading branch information
colomon committed Feb 25, 2010
1 parent 2c553a5 commit 1d4a471
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/Array.pm
Expand Up @@ -39,6 +39,27 @@ augment class Array {
$offset, $size);
@ret;
}

# This should probably handle lazy arrays too.
multi method delete(@array is rw: *@indices) {
self!fill;
my @result;
for @indices -> $index {
my $i = $index >= 0
?? $index
!! +@array + $index;
@result.push(@array[$i]);
undefine @array[$i];

# next seems unnecessary but handles an obscure
# edge case
if $i == (@array - 1) {
@array.pop;
}
}
@array.pop while @array.elems && !defined @array[@array.elems - 1];
return @result;
}
}

our proto sub pop(@array) { @array.pop; }
Expand Down

0 comments on commit 1d4a471

Please sign in to comment.