Course of Raku / Advanced / Containers / Contexts 🆕 / Exercises / Item or list
Solution: Item or list
Here is a possible solution to the task.
Code
my @a = 1, 2, 3;
my $count = 0;
my $x = $(@a);
$count++ for @($x);
say $count;🦋 You can find the source code in the file item-or-list.raku.
Output
3Comments
my $x = $(@a)forces item context, so the whole array is packed into the single scalar$xrather than spread out.@($x)then forces list context, spreading$xback into its three elements. Theforloop therefore runs three times, leaving the counter at3. The two contextualisers are exact opposites:$( )packs a list into one item,@( )unpacks it again.