problem-solving
problem-solving copied to clipboard
Destructuring infinite ranges and sequences
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?
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.
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;
You are right, I was focused on the infinite aspect, I didn't test finite ranges.
This is true for sequences (i.e. ... triple-dot), as well as ranges (i.e. .. double-dot)?