Skip to content

Commit fd6d1f8

Browse files
committed
Rephrase subtitle and introduction of "Sets, Bags, and Mixes" to
emphasize the unorderedness of these containers. Avoid calling them "lists". Add some words about immutable/mutable variants.
1 parent f64f3cf commit fd6d1f8

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

lib/Language/setbagmix.pod

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,33 @@
22
33
=TITLE Sets, Bags, and Mixes
44
5-
=SUBTITLE Unique collections and weighted lists in Perl 6
6-
7-
Often, one wants to work with lists of values that are unique. While calling
8-
L<C<.unique>|unique> on the list repeatedly is certainly an option, better
9-
still is to use a L<B<C<Set>>|Set> or L<B<C<SetHash>>|SetHash>, whose
10-
elements are guaranteed to be unique.
5+
=SUBTITLE Unordered collections of unique and weighted objects in Perl 6
6+
7+
Often you want to collect objects in a container but you do not care
8+
about the order of these objects. For such cases, Perl 6 provides the
9+
I<unordered> collection types L<B<C<Set>>|Set>,
10+
L<B<C<SetHash>>|SetHash>, L<B<C<Bag>>|Bag>, L<B<C<BagHash>>|BagHash>,
11+
L<B<C<Mix>>|Mix>, and L<B<C<MixHash>>|MixHash>. Being unordered, these
12+
containers can be more efficient than L<B<C<Lists>>|List> for looking up
13+
elements or dealing with repeated items.
14+
15+
If you want to get the contained objects (elements) without duplicates
16+
and you only care I<whether> an element is in the collection or not,
17+
you can use a L<B<C<Set>>|Set> or L<B<C<SetHash>>|SetHash>. (If you want
18+
to get rid of duplicates but still preserve order, take a look at the
19+
L<C<unique>|unique> routine for L<B<C<Lists>>|List>.)
1120
1221
=begin comment
1322
=defn Set or SetHash
1423
Collection of distinct objects
1524
=end comment
1625
17-
Other times, one wants to keep track of the number of occurrences of an
18-
item. One could use a hash E<emdash> or use the built-in L<B<C<Bag>>|Bag> or
19-
L<B<C<BagHash>>|BagHash> types (for integer numbers) or the L<B<C<Mix>>|Mix>
20-
or L<B<C<MixHash>>|MixHash> types (for arbitrary numbers).
26+
If you want to keep track of the number of times each object appeared,
27+
you can use a L<B<C<Bag>>|Bag> or L<B<C<BagHash>>|BagHash>. In these
28+
Baggy containers each element has a weight (an unsigned integer)
29+
indicating the number of times the same object has been included in the
30+
collection. The types L<B<C<Mix>>|Mix> and L<B<C<MixHash>>|MixHash> are
31+
similar, but they also allow fractional weights.
2132
2233
=begin comment
2334
=defn Bag or BagHash
@@ -31,6 +42,11 @@ Collection of distinct objects mapped to real-number weights
3142
TODO: Partial type graph showing only QuantHash, Setty, Baggy, Mixy, Set,
3243
SetHash, Bag, BagHash, Mix, MixHash.
3344
45+
L<B<C<Set>>|Set>, L<B<C<Bag>>|Bag>, and L<B<C<Mix>>|Mix> are I<immutable>
46+
types. Use the mutable variants L<B<C<SetHash>>|SetHash>, L<B<C<BagHash>>|BagHash>,
47+
and L<B<C<MixHash>>|MixHash> if you want to add or remove elements after
48+
the container has been constructed.
49+
3450
The six collection classes C<Set>, C<SetHash>, C<Bag>, C<BagHash>, C<Mix>,
3551
C<MixHash>, all share similar semantics.
3652

0 commit comments

Comments
 (0)