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

Re: key/value hash slices summary

Thread Previous | Thread Next
From:
Ruslan Zakirov
Date:
July 2, 2013 09:08
Subject:
Re: key/value hash slices summary
Message ID:
CAMOxC8tx48rRMvCEVhP1_8ZHt2cwEJVnCbcq=j+UNJJ8WDS+ew@mail.gmail.com
On Tue, Jul 2, 2013 at 7:07 AM, Brad Gilbert <b2gills@gmail.com> wrote:

> 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.
>

My goal is to cover as much as possible under hash slices, copy test file,
adjust for arrays and fix functionality in case it's required.


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? )
>

First. You asked three, get three. While we write many examples with hash
on left hand side, but op still returns a list of paired elements not a
hash.


>  ---
>
> 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'
>     }
>

Thanks. For sure it should be.


>  ---
>
> 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'}; )
>

Yes, it's implemented. Will add tests.


>  ---
>
> 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.
>

p5p should decide. I don't think this needs to be.



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

Thank you, Brad. Really helpful feedback.



-- 
Best regards, Ruslan.

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