develooper Front page | perl.perl6.users | Postings from December 2020

Re: The ,= operator

Thread Previous | Thread Next
From:
Joseph Brenner
Date:
December 6, 2020 17:53
Subject:
Re: The ,= operator
Message ID:
CAFfgvXWSntBorz02gDSJsxGNye1_9NRLJK=edGgh11y=x==TQg@mail.gmail.com
ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:

> I am a little late to this conversation, but `,=`
> looks a lot like `push` to me.

Yes that was my first impression, if you read ahead a bit in the
discussion you'll see it explained.

In summary: the <operator>= shortcuts all work in a precisely parallel way, so

@r ,= 'd';

is the same as:

@r = @r , 'd';

And remember, back in perl-land the default behavior is to flatten,
but in raku arrays don't flatten unless you do something to make it
happen-- by default it's oriented toward building up complex
structures like arrays of arrays.   So with this construct, you end up
with the array containing a circular reference to itself in the first
location, which is highly unlikely to be something you want.

Hashes, on the other hand, *don't* tend to flatten by default, so ,=
does something useful with them.

If you wanted to just add an element to the end, you could do one of these:

@r.append('d');
@r = | @r, 'd';
@r.push('d');

The trouble with that last one though, is you may find it surprising
if you're pushing more than a single element, this will get you an
array inside an array:

@r.push(@a);

This on the other hand, behaves just like an @r.append(@a) would:
@r.push(| @a);

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