Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

list

raku

rotor

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?

like image 555
lisprogtor Avatar asked Apr 30 '20 08:04

lisprogtor


2 Answers

my @a = (0,1,2,3,4,5,6,7,8,9);
my @b = @a.rotor(4, :partial)».elems.reverse;
say @a.rotor(|@b);
like image 163
chenyf Avatar answered Sep 28 '22 18:09

chenyf


You can use @a.rotor(2,4,4) or a little more universal @a.rotor(@a % 4,slip 4 xx *). Of course, you could define function

multi batch ( $_, $n, :from-end($)! ) {
    .rotor: .elems % $n, slip $n xx *
}
say batch  ^10,  4,:from-end; #or even
say (^10).&batch(4):from-end;
like image 32
wamba Avatar answered Sep 28 '22 19:09

wamba