Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Applied same optimization logic to other List methods
  • Loading branch information
lizmat committed Jul 25, 2013
1 parent 207cdaf commit fe35215
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/core/List.pm
Expand Up @@ -174,8 +174,8 @@ my class List does Positional {
}

method pop() is parcel {
fail 'Cannot .pop from an infinite list' if self.infinite; #MMD?
my $elems = self.elems;
my $elems = self.gimme(*);
fail 'Cannot .pop from an infinite list' if $!nextiter.defined;
$elems > 0
?? nqp::pop($!items)
!! fail 'Element popped from empty list';
Expand All @@ -190,20 +190,21 @@ my class List does Positional {
}

method roll($n is copy = 1) {
fail "Cannot .roll from an infinite list" if self.infinite; #MMD?
my $elems = self.elems;
my $elems = self.gimme(*);
fail 'Cannot .roll from an infinite list' if $!nextiter.defined;
return unless $elems;
$n = +$Inf if nqp::istype($n, Whatever);
return self.at_pos($elems.rand.floor) if $n == 1;

gather while $n > 0 {
take nqp::atpos($!items, nqp::unbox_i($elems.rand.floor.Int));
$n--;
}
}

method reverse() {
fail 'Cannot .reverse an infinite list' if self.infinite; #MMD?
self.gimme(*);
fail 'Cannot .reverse from an infinite list' if $!nextiter.defined;
my Mu $rev := nqp::list();
my Mu $orig := nqp::clone($!items);
nqp::push($rev, nqp::pop($orig)) while $orig;
Expand All @@ -213,8 +214,8 @@ my class List does Positional {
}

method rotate(Int $n is copy = 1) {
fail 'Cannot .rotate an infinite list' if self.infinite; # MMD?
self.gimme(*);
fail 'Cannot .rotate an infinite list' if $!nextiter.defined;
my Mu $res := nqp::clone($!items);
$n %= nqp::p6box_i(nqp::elems($!items));
if $n > 0 {
Expand Down

0 comments on commit fe35215

Please sign in to comment.