Terry
Terry

Reputation: 1232

Perl 6 assignment hyper operator for nested list doesn't work as expected

Hello I'm trying to use the assignment hyper operator in Perl 6 https://docs.perl6.org/language/operators#Hyper_operators

my (@one, @two) «=» (<1 2 3>,<4 5 6>);
say @one;
say @two;
# Prints nothing (empty array)

# Expected behaviour:
@one=<1 2 3>;
@two=<4 5 6>;
say @one;
say @two;
# Prints [1 2 3] and [4 5 6]

How to make the assignment hyper operator operate correctly? Thanks.

Upvotes: 8

Views: 152

Answers (1)

Holli
Holli

Reputation: 5072

You were close. Just a little bit further in the docs down we find

The zip metaoperator (which is not the same thing as Z) will apply a given infix operator to pairs taken one left, one right, from its arguments.

So

my (@one, @two) Z= <1 2 3>, <4 5 6>;

Here's a benchmark, running on the current developer build. It compares the "magic" variant above with two sequential assignments.

use v6;
use Benchmark;

my %results = timethese(100000, {
    "magic" => sub { my (@one, @two) Z= <1 2 3>, <4 5 6> },
    "plain" => sub { my @one = <1 2 3>; my @two = <4 5 6> },
});

say ~%results;

# magic   1569668462 1569668464 2 0.00002
# plain   1569668464 1569668464 0 0

Upvotes: 10

Related Questions