Perl 6 has proper sets and set-like types, along operators to do proper set operations.
- Set – a collection of unique thingys
- Bag – a collection of unique thingys, but weighted for the count of the number of times something is put the bag
- Mix – a bag that allows fractional weights
These are immutable types. Once you make them, that’s it. Each has a *Hash
version that allows you to change the members, but I’ll ignore those.
$ perl6 > my $set = set( 1, 2, 3, 4 ) set(4, 3, 1, 2) > 4 ∈ $set # member of True > 5 ∈ $set # member of False > 5 ∉ $set # not member of True > set( 2, 3 ) ⊆ $set # subset of True > set( 2, 6 ) ⊆ $set # subset of False
A set is a more natural way to see if a value exists in a list of values. You might each for a hash and use :exists
to check for the key, but a set will do that (even though that’s what
Perl 6 sets are doing for you behind the scenes for now):
my $set = set( <a b c d> ); my $item = 'h'; say "$item is in set" if $item ∈ $set;
Perl 6 has the operators to turn two lists into a set:
$ perl6 > ( 1, 2, 3 ) ∪ ( 4, 5 ) # union set(5, 4, 3, 1, 2) > ( 1, 2, 4 ) ∩ ( 1, 2, 3 ) # intersection set(1, 2) > ( 1, 2, 4 ) ∖ ( 1, 2, 3 ) # difference set(4) > ( 1, 2, 4 ) ⊖ ( 1, 2, 3 ) # symmetric difference set(4, 3)
So far I’ve used the fancy Unicode characters that you’d see in set math, but each of these have Texas (ASCII) versions too:
Texas | Fancy | Codepoint (hex) | Operation |
---|---|---|---|
(elem) | ∈ | U+2208 | member of, $a ∈ $set or $a (elem) $set |
!(elem) | ∉ | U+2209 | not a member of, $a ∉ $set or $a !(elem) $set |
(cont) | ∋ | U+220B | contains, |
!(cont) | ∌ | U+220C | does not contain |
(<=) | ⊆ | U+2286 | subset of or equal to, |
!(<=) | ⊈ | U+2288 | not subset of nor equal to, |
(<) | ⊂ | U+2282 | subset of |
!(<) | ⊄ | U+2284 | not subset of |
(>=) | ⊇ | U+2287 | superset of or equal to, |
!(>=) | ⊉ | U+2289 | not superset of nor equal to, |
(>) | ⊃ | U+2283 | superset of |
!(>) | ⊅ | U+2285 | not superset of |
(>+) | ≼ | U+227C | baggy superset |
!(>+) | ≽ | U+227D | not baggy superset |
There are operators that return sets from two lists:
Texas | Fancy | Codepoint (hex) | Operation |
---|---|---|---|
(|) | ∪ | U+222A | union |
(&) | ∩ | U+2229 | intersection |
(-) | ∖ | U+2216 | difference |
(^) | ⊖ | U+2296 | symmetric difference |
(.) | ⊍ | U+228D | baggy multiplication |
(+) | ⊎ | U+228E | baggy addition |