Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Emphasize the semantics of set operators over Set/Bag/Mix
  • Loading branch information
lizmat committed May 6, 2019
1 parent 6020b81 commit 9ac8c2e
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions doc/Language/setbagmix.pod6
Expand Up @@ -4,25 +4,29 @@
=SUBTITLE Unordered collections of unique and weighted objects in Perl 6
In a nutshell, these classes hold, in general, unordered collections of
objects. Sets only consider if these objects are present or not, bags can
objects, much like an L<object hash|hashmap#index-entry-object_hash>.
The L<QuantHash|/type/QuantHash> role is the role that is implemented by all
of these classes: therefore they are also referenced as C<QuantHash>es.
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 I<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, Perl 6 provides the
Let's elaborate on that. If you want to collect objects in a container buti
you do not care about the order of these objects, Perl 6 provides the
I<unordered> collection types L<Set|/type/Set>,
L<SetHash|/type/SetHash>, L<Bag|/type/Bag>, L<BagHash|/type/BagHash>,
L<Mix|/type/Mix>, and L<MixHash|/type/MixHash>. Being unordered, these
containers can be more efficient than L<Lists|/type/List> for looking up
elements or dealing with repeated items.
On the other hand, if you want to get the contained objects (elements) B<without
duplicates> and you only care I<whether> an element is in the collection
or not, you can use a L<Set|/type/Set> or L<SetHash|/type/SetHash>. If
you want to get rid of duplicates but still preserve order, take a look
On the other hand, if you want to get the contained objects (elements)
B<without duplicates> and you only care I<whether> an element is in the
collection or not, you can use a L<Set|/type/Set> or L<SetHash|/type/SetHash>.
If you want to get rid of duplicates but still preserve order, take a look
at the L<unique|/routine/unique> routine for L<List|/type/List>.
=begin comment
Expand Down Expand Up @@ -66,28 +70,26 @@ associated weights are the 'values':
Bag / BagHash a positive integer 0
Mix / MixHash a non-zero real number 0
=head1 Set-Like operators
=head1 Operators with Set semantics
There are several infix operators devoted to performing common operations on
C<Set-Like> types. Set-Like objects are comprised of L<Set|/type/Set>s,
L<Bag|/type/Bag>s, and L<Mix|/type/Mix>es. Operations that can be performed
on C<Set-Like> types things like unions, set differences and include boolean
checks, like whether an object is an element of a C<Set>, or whether one C<Set>
is a subset of another C<Set>.
There are several infix operators devoted to performing common operations
using Set semantics. This does B<not> mean that the parameters of these
operators must always be C<QuantHash>. It just means that the logic that
is applied to the operators, follows the logic of
L<Set Theory|https://en.wikipedia.org/wiki/Set_theory>.
These infixes can be written using the UTF-8 character that represents the
function (like L<∈|/routine/∈>, or L<∪|/routine/∪>), or with an equivalent ASCII
version like L<(elem)|/routine/(elem)> or L<(^)|/routine/⊖>.
These infixes can be written using the Unicode character that represents the
function (like L<∈|/routine/∈>, or L<∪|/routine/∪>), or with an equivalent
ASCII version like L<(elem)|/routine/(elem)> or L<(^)|/routine/⊖>.
Most of the time, explicitly using C<Set> objects with these infixes is
unnecessary. All the infix operators will work on any objects of type
L<Any|/type/Any> for its arguments (e.g., L<List|/type/List>,
L<Array|/type/Array>, L<Mix|/type/Mix>, etc.) and coerce them to C<Set>
where needed.
So explicitly using C<Set> (or C<Bag> or C<Mix>) objects with these infixes
is unnecessary. All set operators work with all possible arguments. If
necessary, a coercion will take place internally: but in many cases that
is not actually needed.
In some cases, if the type of an argument is a L<Bag|/type/Bag>, the
infix operator will behave in a different but analogous way to the way
it would behave with only C<Set> arguments.
However, if a C<Bag> or C<Mix> is one of the parameters to these set operators,
then the semantics will be upgraded to that type (where C<Mix> supersedes
C<Bag> if both types happen to be used).
=head2 Operators that return C<Bool>
Expand Down

0 comments on commit 9ac8c2e

Please sign in to comment.