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

Re: The ,= operator

Thread Previous | Thread Next
Joseph Brenner
December 6, 2020 17:53
Re: The ,= operator
Message ID:
ToddAndMargo via perl6-users <> 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 = | @r, '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:


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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About