Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructure a List of Pairs

Tags:

raku

rakudo

Consider

.say for (1,2,2).rotor(2=>-1).map( -> ($a, $b) { $a - $b })

which works as expected. However,

.say for (1,2,2).pairs.rotor(2=>-1).map( -> ($a, $b) { $a.value - $b.value })

throws

Too few positionals passed to '<anon>'; expected 2 arguments but got 0 in sub-signature

Is this a bug or am I missing something?

This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03 implementing Perl 6.d.

like image 978
Holli Avatar asked Apr 19 '20 19:04

Holli


People also ask

Can you Destructure a list in Python?

We can also destructure a list, for example, or even a set. You're unlikely to want to perform a destructuring assignment with a set, however, since the order is not guaranteed, and we therefore end up with variables we don't really know the values of.

How do you Destructure a list in Kotlin?

If you want to skip a variable or element in destructuring declarations, then you can use underscore instead of the variable name. As a result, Kotlin won't call the component function for those unused variables. For example, we need only the 1st, 5th and 6th elements in the list.

How do you Destructure an array of objects?

To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element.


1 Answers

It is taking the Pair as a Capture, thus turning the Pair into a named argument:

$ raku -e '(a => 42, b => 666).map: -> |c { dd c }'
\(:a(42))
\(:b(666))

In your example, it then doesn't pass any positional arguments, thus causing the observed execution error.

jnthn++ for pointing this out.

like image 187
Elizabeth Mattijsen Avatar answered Oct 19 '22 11:10

Elizabeth Mattijsen