Course of Raku / Advanced / More about built-in types / Sets, bags, and mixes / Exercises / Common elements
Solution: Common elements
Here is a possible solution to the task.
Code
my $a = set(<apple banana cherry>);
my $b = set(<banana cherry date>);
my $common = $a ∩ $b;
say $common.elems;
say 'apple' ∈ $common;🦋 You can find the source code in the file common-elements.raku.
Output
2
FalseComments
The intersection
$a ∩ $bis itself a set — here{banana, cherry}— so it has2elements. If you prefer ASCII, write(&)instead of∩.Because the intersection is a set, you can ask it the membership question with
∈.appleis only in the first set, so it is not in the intersection, and the test returnsFalse.