develooper Front page | perl.perl5.porters | Postings from July 2013

Re: key/value hash slices summary

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
July 2, 2013 03:07
Subject:
Re: key/value hash slices summary
Message ID:
CAD2L-T2QcwPw+jShiM-5Zj_3QqA4oufCTm_HXdFpG8wLe58FnA@mail.gmail.com
On Mon, Jul 1, 2013 at 8:05 AM, Ruslan Zakirov <ruz@bestpractical.com>
wrote:
> Hi,
>
> Collected summary on the subject (below). At this moment lvalue subs are
> treated as written in the doc, but everything else should be. Edge cases
to
> add to tests are more than welcome.
>
> Key/value hash slices and index/value array slices
>   Syntax
>     Just like existing slice operations.
>
>         my %sub = %hash{'a', 'b', 'c'};
>
>   Why a new syntax?
>     New syntax fits within existing syntax for slices and completes the
>     picture.
>
>         my $scalar = $h{'a'};
>         my @list = @h{'a', 'b'};
>         my %hash = %h{'a', 'b'};
>
>     First two from above exist for ages, the third fits natural. I don't
see
>     any other reasonable behaviour for the third.
>
>   Why not a function?
>         my %second = slice %first, qw/a c/;
>
>     It's sure possible to implement this as function, but I feel it fits.
>
>   But Perl 6?
>     Sigils in Perl 6 have different meaninging and it's radical change
from
>     Perl 5, so this argument is hardly applicable..
>
> ...

There doesn't seem to be a t/op/kvaslice.t
for checking %a[1,2,3] on arrays, even though
you added that functionality.

After checking
https://github.com/ruz/perl5/blob/ruz/kvhslice-op/t/op/kvhslice.t
for missing edge cases, I came up with a few.

    my %h = map { $_ => uc $_ } 'a'..'z';

    @a = %h{ ('c') x 3 };

Which of the following is true?

    ok eq_array \@a, [ ( 'c', 'C' ) x 3 ];
    ok eq_array \@a, [ 'c', 'C' ];

( Which do we want? )

 ---

These three lines should be equivalent:

    @a = \%h{ qw'c d e' };
    @a = \( %h{ qw'c d e' } );
    @a = map{ \$_ } %h{ qw'c d e' };

for consistency with:

    @a = \@h{ qw'c d e' };

Example test:

    {
        my @a = \%h{ qw'c d e' };

        my $ok = 1;
        for( @a ){
            if( !ref ){
                $ok = 0;
                last;
            }
        }

        ok $ok;
        is join( ':', map{ $$_ } @a ), 'c:C:d:D:e:E'
    }

 ---

There is currently no tests for

    my $r = \%h;

    my %new = %$r{qw'c d e'};

Is it implemented?

( again for consistency with @$r{qw'c d e'}; )

 ---

Before it becomes an issue, I don't think this needs to be
guarded by a `use feature 'kvhslice';` since it was previously
a syntax error.

 ---

I would like to say that I am now mildly in favor of this feature.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About