Types, classes & roles
Sets & Bags
FullImmutable collections — a Set of distinct elements, a Bag with counts.
A Set is an unordered collection of distinct elements; a Bag is the same but keeps a count of each. Both are immutable and pair with the set operators.
Set — distinct elements #
set(...) deduplicates. Membership and size are O(1); iteration order is undefined, so sort when you need determinism.
my $s = set(1, 2, 2, 3); say $s.elems; say $s.keys.sort;
Output
3
(1 2 3)Bag — elements with counts #
bag(...) counts occurrences. Subscripting returns an element's count, .total the sum of all counts.
my $b = bag(<a a b>); say $b.elems; say $b<a>; say $b.total;
Output
2
2
3.elems is the number of distinct keys (2: a and b); .total is the number of items counted (3).
Notes #
- Any list coerces with
.Set/.Bag:<a b b c>.Bagbuilds the bag directly. Mixis the third member — like aBagbut with fractional/real weights instead of integer counts.- These are immutable; their mutable cousins are
SetHashandBagHash, which you can add to and remove from.