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
False

Comments

  1. The intersection $a ∩ $b is itself a set — here {banana, cherry} — so it has 2 elements. If you prefer ASCII, write (&) instead of .

  2. Because the intersection is a set, you can ask it the membership question with . apple is only in the first set, so it is not in the intersection, and the test returns False.

Course navigation

Common elements   |   Count in a bag