Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

raku

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.

like image 280
Terry Avatar asked Sep 27 '19 18:09

Terry


People also ask

How many operators are there in Perl?

Here 4 and 5 are called operands and + is called operator. Perl language supports many operator types, but following is a list of important and most frequently used operators − Lets have a look at all the operators one by one.

How to do arithmetic operations in Perl?

Perl arithmetic operators deal with basic math such as adding, subtracting, multiplying, diving, etc. To add (+ ) or subtract (-) numbers, you would do something as follows: To multiply or divide numbers, you use divide (/) and multiply (*) operators as follows:

What does Perl have that C doesn't?

Here's what perlhas that C doesn't: The exponentiation operator. The exponentiation assignment operator. The null list, used to initialize an array to null. Concatenation of two strings. The concatenation assignment operator. eq String equality (== is numeric equality). For a mnemonic just think of "eq" as a string.

How to construct a list in Perl?

A Perl list is a sequence of scalar values. You use parenthesis and comma operators to construct a list. Each value is the list is called list element. List elements are indexed and ordered. You can refer to each element by its position. The first list () is an empty list. The second list (10,20,30) is a list of integers.


1 Answers

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
like image 78
Holli Avatar answered Sep 18 '22 13:09

Holli