Re: stashing an array in a hash and yanking it back out

2020-03-19 Thread William Michels via perl6-users
Hi all,

Andy Bach contacted me off list, to say my postscript comment about
"prepending" (versus "appending") to a hash object was nonsensical. I
agree.

Hashes in Raku/Perl6 are guaranteed to be unordered upon return. So
once they are declared, it doesn't make sense to think of them as a
(double-ended) linear array of pairs.

--Bill.

P.S. Link to the ",=" Postfix operator:

https://docs.raku.org/routine/,=





On Thu, Mar 19, 2020 at 10:33 AM William Michels  wrote:
>
> Okay, here's another (simpler?) approach using the ",= " postfix operator:
>
> mbook:~ homedir$ perl6
> To exit type 'exit' or '^D'
> > my %stash;
> {}
> > my @monsters = << godzilla grendel wormface blob >>;
> [godzilla grendel wormface blob]
> > my @rabbits = << bugs peter easter >>;
> [bugs peter easter]
> > %stash ,= monsters => @monsters
> {monsters => [godzilla grendel wormface blob]}
> > %stash ,= :@rabbits
> {monsters => [godzilla grendel wormface blob], rabbits => [bugs peter easter]}
> > say %stash
> {monsters => [godzilla grendel wormface blob], rabbits => [bugs peter easter]}
> > exit
> mbook:~ homedir$ perl6 -v
> This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1
> implementing Perl 6.d.
>
> HTH, Bill.
>
> PS. Note, above shows how to 'postfix' (i.e. append), but I'm still
> unclear on how to prepend to a pre-existing hash.
>
>
> On Tue, Mar 17, 2020 at 11:15 AM Joseph Brenner  wrote:
> >
> > Yes you're right: I could've sworn I tried that in the repl a minute
> > ago and it worked, but actually it's a no-op and appends nothing to
> > the hash.
> >
> > This is okay, doing it the other way (without the inner parens around
> > the colonpair) is not:
> >
> > ny %stash;
> > my @monsters = << godzilla grendel wormface blob >>;
> > my @rabbits = << bugs peter easter >>;
> >
> > %stash.append( (:@monsters) );
> > %stash.append( (:@rabbits) );
> >
> > say %stash;
> >
> >
> > On 3/17/20, Vadim Belman  wrote:
> > >
> > > Joseph, you've got yourself into a trap I fell into yesterday.
> > > %stash.append( :@stuff ) syntax is about calling append method with a 
> > > named
> > > parameter stuff whereas append works with positionals only. So, your case
> > > should be written:
> > >
> > > %stash.append( (:@stuff) );
> > >
> > > Which is apparently more cumbersome. In either case, use of colons is not
> > > always about saving a character or two. Sometimes it's about readability,
> > > sometimes about elegance. Overuse is surely bad, but overuse of anything 
> > > is
> > > bad, for that matter. :)
> > >
> > > Best regards,
> > > Vadim Belman
> > >
> > >> On Mar 17, 2020, at 1:09 PM, Joseph Brenner  wrote:
> > >>
> > >>> Though I've no idea what those colons are/are not doing.
> > >>
> > >> Those are "colon pairs" (which I've relearned around three times now...):
> > >>
> > >>   https://docs.raku.org/language/glossary#index-entry-Colon_Pair
> > >> 
> > >>
> > >> Except for this colon:
> > >>
> > >>  %stash.append: (rocks => @rocks);
> > >>
> > >> Which is a short hand for this:
> > >>
> > >>  %stash.append( (rocks => @rocks) );
> > >>
> > >> As an aside: it's a minor style point, but I think a lot of
> > >> us overuse that trick-- it saves a character, but the explicit
> > >> parens are more flexible.
> > >>
> > >> Notably this works fine, so here it doesn't even save any
> > >> characters:
> > >>
> > >>  %stash.append( :@stuff );
> > >>
> > >>
> > >
> > >


Re: stashing an array in a hash and yanking it back out

2020-03-19 Thread William Michels via perl6-users
Okay, here's another (simpler?) approach using the ",= " postfix operator:

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
> my %stash;
{}
> my @monsters = << godzilla grendel wormface blob >>;
[godzilla grendel wormface blob]
> my @rabbits = << bugs peter easter >>;
[bugs peter easter]
> %stash ,= monsters => @monsters
{monsters => [godzilla grendel wormface blob]}
> %stash ,= :@rabbits
{monsters => [godzilla grendel wormface blob], rabbits => [bugs peter easter]}
> say %stash
{monsters => [godzilla grendel wormface blob], rabbits => [bugs peter easter]}
> exit
mbook:~ homedir$ perl6 -v
This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1
implementing Perl 6.d.

HTH, Bill.

PS. Note, above shows how to 'postfix' (i.e. append), but I'm still
unclear on how to prepend to a pre-existing hash.


On Tue, Mar 17, 2020 at 11:15 AM Joseph Brenner  wrote:
>
> Yes you're right: I could've sworn I tried that in the repl a minute
> ago and it worked, but actually it's a no-op and appends nothing to
> the hash.
>
> This is okay, doing it the other way (without the inner parens around
> the colonpair) is not:
>
> ny %stash;
> my @monsters = << godzilla grendel wormface blob >>;
> my @rabbits = << bugs peter easter >>;
>
> %stash.append( (:@monsters) );
> %stash.append( (:@rabbits) );
>
> say %stash;
>
>
> On 3/17/20, Vadim Belman  wrote:
> >
> > Joseph, you've got yourself into a trap I fell into yesterday.
> > %stash.append( :@stuff ) syntax is about calling append method with a named
> > parameter stuff whereas append works with positionals only. So, your case
> > should be written:
> >
> > %stash.append( (:@stuff) );
> >
> > Which is apparently more cumbersome. In either case, use of colons is not
> > always about saving a character or two. Sometimes it's about readability,
> > sometimes about elegance. Overuse is surely bad, but overuse of anything is
> > bad, for that matter. :)
> >
> > Best regards,
> > Vadim Belman
> >
> >> On Mar 17, 2020, at 1:09 PM, Joseph Brenner  wrote:
> >>
> >>> Though I've no idea what those colons are/are not doing.
> >>
> >> Those are "colon pairs" (which I've relearned around three times now...):
> >>
> >>   https://docs.raku.org/language/glossary#index-entry-Colon_Pair
> >> 
> >>
> >> Except for this colon:
> >>
> >>  %stash.append: (rocks => @rocks);
> >>
> >> Which is a short hand for this:
> >>
> >>  %stash.append( (rocks => @rocks) );
> >>
> >> As an aside: it's a minor style point, but I think a lot of
> >> us overuse that trick-- it saves a character, but the explicit
> >> parens are more flexible.
> >>
> >> Notably this works fine, so here it doesn't even save any
> >> characters:
> >>
> >>  %stash.append( :@stuff );
> >>
> >>
> >
> >