Course of Raku / Advanced / Subroutines / Signatures / Exercises / Slurpy named arguments
Solution: Slurpy named arguments
Here is a possible solution to the task.
Code
sub describe($name, *%opts) {
my $details = %opts.sort.map({ "{.key}={.value}" }).join(', ');
"$name: $details";
}
say describe('Anna', colour => 'red', size => 5);🦋 You can find the source code in the file slurpy-hash.raku.
Output
Anna: colour=red, size=5Comments
The fixed positional parameter
$nameis filled first, and the slurpy*%optsthen gathers every remaining named argument into a hash.%opts.sortorders the pairs by key, so the output is deterministic —colourcomes beforesize. The.mapturns each pair into akey=valuestring with.keyand.value, and.join(', ')glues them together.The final string interpolates
$nameand the assembled$details, producingAnna: colour=red, size=5.