txt: ## setbagmix.md
  Sets, bags, and mixes
  
  Unordered collections of unique and weighted objects in Raku
  
  # [Introduction](#Sets,_bags,_and_mixes "go to top of document")[§](#Introduction "direct link")
  
  The six collection classes are [`Set`](/type/Set), [`SetHash`](/type/SetHash), [`Bag`](/type/Bag), [`BagHash`](/type/BagHash), [`Mix`](/type/Mix) and [`MixHash`](/type/MixHash). They all share similar semantics.
  
  In a nutshell, these classes hold, in general, unordered collections of objects, much like an [object hash](/type/Hash). The [`QuantHash`](/type/QuantHash) role is the role that is implemented by all of these classes: therefore they are also referenced as [`QuantHash`](/type/QuantHash)es.
  
  [`Set`](/type/Set) and [`SetHash`](/type/SetHash) also implement the [`Setty`](/type/Setty) role, [`Bag`](/type/Bag) and [`BagHash`](/type/BagHash) implement the [`Baggy`](/type/Baggy) role, [`Mix`](/type/Mix) and [`MixHash`](/type/MixHash) implement the [`Mixy`](/type/Mixy) role (which itself implements the [`Baggy`](/type/Baggy) role).
  
  Sets only consider if objects in the collection are present or not, bags can hold several objects of the same kind, and mixes also allow fractional (and negative) weights. The regular versions are immutable, the *-Hash* versions are mutable.
  
  Let's elaborate on that. If you want to collect objects in a container but you do not care about the order of these objects, Raku provides these *unordered* collection types. Being unordered, these containers can be more efficient than [`List`](/type/List)s or [`Array`](/type/Array)s for looking up elements or dealing with repeated items.
  
  On the other hand, if you want to get the contained objects (elements) **without duplicates** and you only care *whether* an element is in the collection or not, you can use a [`Set`](/type/Set) or [`SetHash`](/type/SetHash).
  
  If you want to get rid of duplicates but still preserve order, take a look at the [unique](/routine/unique) routine for [`List`](/type/List).
  
  If you want to keep track of the **number of times each object appeared**, you can use a [`Bag`](/type/Bag) or [`BagHash`](/type/BagHash). In these [`Baggy`](/type/Baggy) containers each element has a weight (an unsigned integer) indicating the number of times the same object has been included in the collection.
  
  The types [`Mix`](/type/Mix) and [`MixHash`](/type/MixHash) are similar to [`Bag`](/type/Bag) and [`BagHash`](/type/BagHash), but they also allow **fractional and negative weights**.
  
  [`Set`](/type/Set), [`Bag`](/type/Bag), and [`Mix`](/type/Mix) are *immutable* types. Use the mutable variants [`SetHash`](/type/SetHash), [`BagHash`](/type/BagHash), and [`MixHash`](/type/MixHash) if you want to add or remove elements after the container has been constructed.
  
  For one thing, as far as they are concerned, identical objects refer to the same element – where identity is determined using the [WHICH](/routine/WHICH) method (i.e. the same way that the [===](/routine/===) operator checks identity). For value types like [`Str`](/type/Str), this means having the same value; for reference types like [`Array`](/type/Array), it means referring to the same object instance.
  
  Secondly, they provide a Hash-like interface where the actual elements of the collection (which can be objects of any type) are the 'keys', and the associated weights are the 'values':
  
  | type of $a | value of $a{$b} if $b is an element | value of $a{$b} if $b is not an element |
  | --- | --- | --- |
  | Set / SetHash | True | False |
  | Bag / BagHash | a positive integer | 0 |
  | Mix / MixHash | a non-zero real number | 0 |
  
  # [Operators with set semantics](#Sets,_bags,_and_mixes "go to top of document")[§](#Operators_with_set_semantics "direct link")
  
  There are several infix operators devoted to performing common operations using [`QuantHash`](/type/QuantHash) semantics. Since that is a mouthful, these operators are usually referred to as "set operators".
  
  This does **not** mean that the parameters of these operators must always be [`Set`](/type/Set), or even a more generic [`QuantHash`](/type/QuantHash). It just means that the logic that is applied to the operators is the logic of [Set Theory](https://en.wikipedia.org/wiki/Set_theory).
  
  These infixes can be written using the Unicode character that represents the function (like `∈` or `∪`), or with an equivalent ASCII version (like `(elem)` or `(|)`).
  
  So explicitly using [`Set`](/type/Set) (or [`Bag`](/type/Bag) or [`Mix`](/type/Mix)) objects with these infixes is unnecessary. All set operators work with all possible arguments, including (since 6.d) those that are not explicitly set-like. If necessary, a coercion will take place internally: but in many cases that is not actually needed.
  
  However, if a [`Bag`](/type/Bag) or [`Mix`](/type/Mix) is one of the parameters to these set operators, then the semantics will be upgraded to that type (where [`Mix`](/type/Mix) supersedes [`Bag`](/type/Bag) if both types happen to be used).
  
  ## [Set operators that return](#Sets,_bags,_and_mixes "go to top of document") [`Bool`](/type/Bool)[§](#Set_operators_that_return_Bool "direct link")
  
  ### [infix (elem), infix ∈](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(elem),_infix_∈ "direct link")
  
  Returns `True` if `$a` is an **element** of `$b`, else `False`. [More information](/language/operators#infix_(elem),_infix_∈), [Wikipedia definition](https://en.wikipedia.org/wiki/Element_(mathematics)#Notation_and_terminology).
  
  ### [infix ∉](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_∉ "direct link")
  
  Returns `True` if `$a` is **not** an element of `$b`, else `False`. [More information](/language/operators#infix_∉), [Wikipedia definition](https://en.wikipedia.org/wiki/Element_(mathematics)#Notation_and_terminology).
  
  ### [infix (cont), infix ∋](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(cont),_infix_∋ "direct link")
  
  Returns `True` if `$a` **contains** `$b` as an element, else `False`. [More information](/language/operators#infix_(cont),_infix_∋), [Wikipedia definition](https://en.wikipedia.org/wiki/Element_(mathematics)#Notation_and_terminology).
  
  ### [infix ∌](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_∌ "direct link")
  
  Returns `True` if `$a` does **not** contain `$b`, else `False`. [More information](/language/operators#infix_∌), [Wikipedia definition](https://en.wikipedia.org/wiki/Element_(mathematics)#Notation_and_terminology).
  
  ### [infix (<=), infix ⊆](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(<=),_infix_⊆ "direct link")
  
  Returns `True` if `$a` is a **subset** or is equal to `$b`, else `False`. [More information](/language/operators#infix_(<=),_infix_⊆), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix ⊈](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_⊈ "direct link")
  
  Returns `True` if `$a` is **not** a **subset** nor equal to `$b`, else `False`. [More information](/language/operators#infix_⊈), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix (<), infix ⊂](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(<),_infix_⊂ "direct link")
  
  Returns `True` if `$a` is a **strict subset** of `$b`, else `False`. [More information](/language/operators#infix_(<),_infix_⊂), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix ⊄](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_⊄ "direct link")
  
  Returns `True` if `$a` is **not** a **strict subset** of `$b`, else `False`. [More information](/language/operators#infix_⊄), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix (>=), infix ⊇](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(>=),_infix_⊇ "direct link")
  
  Returns `True` if `$a` is a **superset** of or equal to `$b`, else `False`. [More information](/language/operators#infix_(>=),_infix_⊇), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix ⊉](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_⊉ "direct link")
  
  Returns `True` if `$a` is **not** a **superset** nor equal to `$b`, else `False`. [More information](/language/operators#infix_⊉), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix (>), infix ⊃](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(>),_infix_⊃ "direct link")
  
  Returns `True` if `$a` is a **strict superset** of `$b`, else `False`. [More information](/language/operators#infix_(>),_infix_⊃), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix ⊅](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_⊅ "direct link")
  
  Returns `True` if `$a` is **not** a **strict superset** of `$b`, else `False`. [More information](/language/operators#infix_⊅), [Wikipedia definition](https://en.wikipedia.org/wiki/Subset#Definitions).
  
  ### [infix (==), infix ≡](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(==),_infix_≡ "direct link")
  
  Returns `True` if `$a` and `$b` are **identical**, else `False`. [More information](/language/operators#infix_(==),_infix_≡), [Wikipedia definition](https://en.wikipedia.org/wiki/Equality_(mathematics)#Equality_in_set_theory).
  
  Available as of the 2020.07 Rakudo compiler release. Users of older versions of Rakudo can install the [Set::Equality](https://raku.land/zef:lizmat/Set::Equality) module for the same functionality.
  
  ### [infix ≢](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_≢ "direct link")
  
  Returns `True` if `$a` and `$b` are **not identical**, else `False`. [More information](/language/operators#infix_≢), [Wikipedia definition](https://en.wikipedia.org/wiki/Equality_(mathematics)#Equality_in_set_theory).
  
  Available as of the 2020.07 Rakudo compiler release. Users of older versions of Rakudo can install the [Set::Equality](https://raku.land/zef:lizmat/Set::Equality) module for the same functionality.
  
  ## [Set operators that return a](#Sets,_bags,_and_mixes "go to top of document") [`QuantHash`](/type/QuantHash)[§](#Set_operators_that_return_a_QuantHash "direct link")
  
  ### [infix (|), infix ∪](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(|),_infix_∪ "direct link")
  
  Returns the **union** of all its arguments. [More information](/language/operators#infix_(|),_infix_∪), [Wikipedia definition](https://en.wikipedia.org/wiki/Union_(set_theory)).
  
  ### [infix (&), infix ∩](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(&),_infix_∩ "direct link")
  
  Returns the **intersection** of all of its arguments. [More information](/language/operators#infix_(&),_infix_∩), [Wikipedia definition](https://en.wikipedia.org/wiki/Intersection_(set_theory)).
  
  ### [infix (-), infix ∖](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(-),_infix_∖ "direct link")
  
  Returns the **set difference** of all its arguments. [More information](/language/operators#infix_(-),_infix_∖), [Wikipedia definition](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement).
  
  ### [infix (^), infix ⊖](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(^),_infix_⊖ "direct link")
  
  Returns the **symmetric set difference** of all its arguments. [More information](/language/operators#infix_(^),_infix_⊖), [Wikipedia definition](https://en.wikipedia.org/wiki/Symmetric_difference).
  
  ## [Set operators that return a](#Sets,_bags,_and_mixes "go to top of document") [`Baggy`](/type/Baggy)[§](#Set_operators_that_return_a_Baggy "direct link")
  
  ### [infix (.), infix ⊍](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(.),_infix_⊍ "direct link")
  
  Returns the Baggy **multiplication** of its arguments. [More information](/language/operators#infix_(.),_infix_⊍).
  
  ### [infix (+), infix ⊎](#Sets,_bags,_and_mixes "go to top of document")[§](#infix_(+),_infix_⊎ "direct link")
  
  Returns the Baggy **addition** of its arguments. [More information](/language/operators#infix_(+),_infix_⊎).
  
  ## [Terms related to set operators](#Sets,_bags,_and_mixes "go to top of document")[§](#Terms_related_to_set_operators "direct link")
  
  ### [term ∅](#Sets,_bags,_and_mixes "go to top of document")[§](#term_∅ "direct link")
  
  The empty set. [More information](/language/terms#term_∅), [Wikipedia definition](https://en.wikipedia.org/wiki/Empty_set).
  