Search code examples
listrakurotor

raku What is a better way to do rotor from the end?


If data at the end of a list are more important, and I want :partial list to be left at the beginning of the list while preserving the initial order, I do this:

> my @a = (0,1,2,3,4,5,6,7,8,9);
[0 1 2 3 4 5 6 7 8 9]
> say @a.rotor(4, :partial)
((0 1 2 3) (4 5 6 7) (8 9)) # not what I want; important data at end gets cut off;
> say @a.reverse.rotor(4, :partial).reverse.map({$_.reverse});
((0 1) (2 3 4 5) (6 7 8 9)) # this is what I want

Is there a way to avoid the 3 "reverse" operations? Is it possible to add a :fromEnd adverb?


Solution

  • my @a = (0,1,2,3,4,5,6,7,8,9);
    my @b = @a.rotor(4, :partial)».elems.reverse;
    say @a.rotor(|@b);