problem-solving icon indicating copy to clipboard operation
problem-solving copied to clipboard

Destructuring infinite ranges and sequences

Open CIAvash opened this issue 5 years ago • 4 comments

Destructuring inifinte ranges and sequences does not work in subroutine signatures.

Assignment seems to work:

my ($x, **@xs) = 1..*;

But binding doesn't:

my ($x, **@xs) := 1..*;

It gives this error: Too few positionals passed to '<unit>'; expected at least 1 argument but got only 0 Which means it won't work in sub signatures:

sub f ([$x, **@xs]) {
    dd $x, @xs;
}
f 1..*;

It gives this error: Too few positionals passed to 'f'; expected at least 1 argument but got only 0 in sub-signature

Should it work?

CIAvash avatar Aug 09 '20 11:08 CIAvash

To give some context, I asked this question 4 years ago on IRC and none of the answers worked with infinite ranges and sequences.

I was trying to see if I could implement the intersperse function in Raku like how it's implemented in Haskell. You can see the code here.

CIAvash avatar Aug 09 '20 11:08 CIAvash

It's a bit weird because this also doesn't work:

my ($x, **@xs) := 1..100;

So it isn't just about infinite ranges. That being said, this works:

my ($x, **@xs) := (1..100).list;

AlexDaniel avatar Aug 09 '20 12:08 AlexDaniel

You are right, I was focused on the infinite aspect, I didn't test finite ranges.

CIAvash avatar Aug 09 '20 13:08 CIAvash

This is true for sequences (i.e. ... triple-dot), as well as ranges (i.e. .. double-dot)?

jubilatious1 avatar Nov 28 '23 17:11 jubilatious1