To select multiple elements from an array in perl6, it is easy: just use a list of indices:
> my @a = < a b c d e f g >;
> @a[ 1,3,5 ]
(b d f)
But to de-select those elements, I had to use Set:
> say @a[ (@a.keys.Set (-) (1,3,5)).keys.sort ]
(a c e g)
I am wondering if there is an easier way because the arrays I use are often quite large?
I think the operator code is self-explanatory if you know each of the P6 constructs it uses. If anyone would appreciate an explanation of it beyond the following, let me know in the comments.
I'll start with the two aspects that generate the call to
not-at.*akaWhateverFrom the
Whateverdoc page:*is indeed used in the above as an operand. In this case it's the left argument (corresponding to the$elemsparameter) of the infixnot-atoperator that I've just created.The next question is, will the compiler do the transform? The compiler decides based on whether the operator has an explicit
*as the parameter corresponding to the*argument. If I'd written*instead of$elemsthen that would have madenot-atone of the few operators that wants to directly handle the*and do whatever it chooses to do and the compiler would directly call it. But I didn't. I wrote$elems. So the compiler does the transform I'll describe next.The transform builds a new
WhateverCodearound the enclosing expression and rewrites theWhateveras "it" aka the topic aka$_instead. So in this case it turns this:into this:
What
[...]as a subscript doesThe
[...]in@a[...]is aPositional(array/list) subscript. This imposes several evaluation aspects, of which two matter here:"it" aka the topic aka
$_is set to the length of the list/array.If the content of the subscript is a
Callableit gets called. TheWhateverCodegenerated as explained above is indeed aCallableso it gets called.So this:
becomes this:
which turns into this: