Permalink
Browse files

Implement Parcel.rotate

One wonders whether the internal logic would warrant an nqp::rotate op, which
could then be shared with List.rotate
  • Loading branch information...
lizmat committed Oct 22, 2013
1 parent e9830d3 commit b0c90ed3d2f6a2c7b86ae21cea87940bfc5140f6
Showing with 14 additions and 0 deletions.
  1. +14 −0 src/core/Parcel.pm
View
@@ -35,6 +35,20 @@ my class Parcel does Positional { # declared in BOOTSTRAP
method list() { nqp::p6list(nqp::clone($!storage), List, Mu) }
method lol() { nqp::p6list(nqp::clone($!storage), LoL, Mu) }
+ method rotate (Int $n is copy = 1) {
+ my Mu $storage := nqp::clone($!storage);
+ $n %= nqp::p6box_i(nqp::elems($!storage));
+ if $n > 0 {
+ nqp::push($storage, nqp::shift($storage)) while $n--;
+ }
+ elsif $n < 0 {
+ nqp::unshift($storage, nqp::pop($storage)) while $n++;
+ }
+ my $parcel := nqp::create(self.WHAT);
+ nqp::bindattr($parcel, Parcel, '$!storage', $storage);
+ $parcel;
+ }
+
method at_pos(Parcel:D: \x) is rw { self.flat.at_pos(x); }
proto method postcircumfix:<[ ]>(|) { * }

0 comments on commit b0c90ed

Please sign in to comment.