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?
sub infix:<not-at> ($elems, @not-ats) {
my $at = 0;
flat gather for @not-ats -> $not-at {
when $at < $not-at { take $at++ xx $not-at - $at }
NEXT { $at++ }
LAST { take $at++ xx $elems - $not-at - 1 }
}
}
my @a = < a b c d e f g >;
say @a[ * not-at (1, 3, 5) ]; # (a c e g)
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
.
*
aka Whatever
From the Whatever
doc page:
When
*
is used in term position, that is, as an operand, in combination with most operators, the compiler will transform the expression into a closure of typeWhateverCode
*
is indeed used in the above as an operand. In this case it's the left argument (corresponding to the $elems
parameter) of the infix not-at
operator 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 $elems
then that would have made not-at
one 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 WhateverCode
around the enclosing expression and rewrites the Whatever
as "it" aka the topic aka $_
instead. So in this case it turns this:
* not-at (1,3,5)
into this:
{ $_ not-at (1,3,5) }
[...]
as a subscript doesThe [...]
in @a[...]
is a Positional
(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 Callable
it gets called. The WhateverCode
generated as explained above is indeed a Callable
so it gets called.
So this:
@a[ * not-at (1,3,5) ]
becomes this:
@a[ { $_ not-at [1,3,5] } ]
which turns into this:
@a[ { infix:not-at(7, [1,3,5]) } ]