develooper Front page | perl.perl5.porters | Postings from June 2021

Re: idea: everything-slices

Thread Previous | Thread Next
mah.kitteh via perl5-porters
June 20, 2021 02:31
Re: idea: everything-slices
Message ID:
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Saturday, June 19th, 2021 at 8:53 PM, Ricardo Signes <> wrote:

> Porters,
> I feel confident that this should be an RFC, but our process says "post to p5p before writing up an RFC," and "write something less format" sounds great. So:
> I would like to propose a new piece of syntax. It's not my own original idea, it's come from a few places and individuals independently, but I'm the one who's ready to go to bat for it here. It is the everything slice.
> An everything slice is a slice that includes every element in an aggregate. We can hash out syntax, but let's start by saying "it's a slice where the subscript is replaced by a literal asterisk."

This is my only comment, though I tried to carefully read your proposal. In principle I personally like it, but don't use slices as often as I probably should.

I am detecting some conflcit here with slice (every "even" indexed element") and "every" element. Is it correct to say that '*' below means every "tuple" (pair)?

For example, I can do:
my %hash = (qw/one 1 two 2 three 3);

And similarly, the following 2 are equivalent:

my @array1 = %hash;
my @array2 = (qw/one 1 two 2 three 3/);

Am I reading this right that, the '*' notation is actually sending a tuple of each $key/$val pair? If this is the case, my only suggestion is that there be some other semantic hint other than the "{}" that this is doing the "slice" (or "pair-wise") thing. It does seem a lot more clear when keys are explicitly written as the "slice". With this in mind "slice notation" is a thing and it might behoove us to investigate this a bit futher. But I do personally like the idea of indicating via "{}" to 'send all pairs".

And FWIW, bash associative arrays us "@". Which would probably be confusing for us. E.g.,

for i in "${!array[@]}"
echo "key : $i" echo "value: ${array[$i]}"

I do particularly like this general idea expressed below as,

@array[*] = @hundred_items; # @array remains 10 long

But I feel like going back and looking at well developed "slice notation" is likely in order. Even if it's to take a look at PDL, which surely has crossed this bridge.


> So:
> @array[*];  # equivalent to @array[ 0 .. $#array ];
> %array[*];  # equivalent to %array[ 0 .. $#array ];
> @hash{*};   # equivalent to @hash{ keys %hash };
> %hash{*};   # equivalent to %hash{ keys %hash };
> No, this is not the same as writing @array or %hash. For one thing, when the \ operator is applied to a slice, it produces a list of references to individual elements, not a reference to the container. Similarly,
> This provides an easy way to get all the keys and values of an array, which pairs nicely with multi-target for:
> for my ($i, $v) (@array[*]) {
>   ...
> }
> You can get a reverse lookup for arrays:
> my %index_for = reverse %array[*];
> You can assign a large list to an array without growing it:
> @array = (1 .. 10);
> @array[*] = @hundred_items; # @array remains 10 long
> So: if this is going to get shot down before it gets to an RFC, now's the time, but honestly I think you'll probably get to see me write an RFC for this within a week.
> --
> rjbs
Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About