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=5

Comments

  1. The fixed positional parameter $name is filled first, and the slurpy *%opts then gathers every remaining named argument into a hash.

  2. %opts.sort orders the pairs by key, so the output is deterministic — colour comes before size. The .map turns each pair into a key=value string with .key and .value, and .join(', ') glues them together.

  3. The final string interpolates $name and the assembled $details, producing Anna: colour=red, size=5.

Course navigation

Slurpy named arguments   |   Quiz — Slurpy parameters