2
2
3
3
= TITLE Sets, Bags, and Mixes
4
4
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> .)
11
20
12
21
= begin comment
13
22
= defn Set or SetHash
14
23
Collection of distinct objects
15
24
= end comment
16
25
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.
21
32
22
33
= begin comment
23
34
= defn Bag or BagHash
@@ -31,6 +42,11 @@ Collection of distinct objects mapped to real-number weights
31
42
TODO: Partial type graph showing only QuantHash, Setty, Baggy, Mixy, Set,
32
43
SetHash, Bag, BagHash, Mix, MixHash.
33
44
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
+
34
50
The six collection classes C < Set > , C < SetHash > , C < Bag > , C < BagHash > , C < Mix > ,
35
51
C < MixHash > , all share similar semantics.
36
52
0 commit comments