raku What is a better way to do rotor from the end?
my @a = (0,1,2,3,4,5,6,7,8,9);
my @b = @a.rotor(4, :partial)».elems.reverse;
say @a.rotor(|@b);
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;
Taking the last question first: Yes, it is possible, but it would probably be called :end
for consistency, and no I don't think it is likely to be added. But then I could be wrong :-)
I'm not sure how golfed your example is, but if you're really working from an array, why not stuff it first with values to make it evenly divisible by 4, and then remove them afterwards again:
my @a = ^10;
@a.splice(0,0, Any xx @a % 4);
say @a.batch(4); # (((Any) (Any) 0 1) (2 3 4 5) (6 7 8 9))
Note I've used the shorter ^10
(which is a range from 0
to 10
, excluding the endpoint). And I've used the simpler batch
method, as we are sure there won't be any partial lists. In any case, no reverse
is needed, just checking values afterwards.
Mind you, the reverse
method on an array, is relatively cheap, as it does not actually move any values.