4
4
5
5
= SUBTITLE Unique collections and weighted lists in Perl 6
6
6
7
- Often, one wants to work with lists of values that are unique. While
8
- calling L < C < .unique > |unique> on the list repeatedly is certainly an option,
9
- better still is to use a L < B < C < Set > > |Set> or L < B < C < SetHash > > |SetHash> ,
10
- whose elements are guaranteed to be unique.
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.
11
11
12
12
= begin comment
13
13
= defn Set or SetHash
@@ -16,9 +16,8 @@ Collection of distinct objects
16
16
17
17
Other times, one wants to keep track of the number of occurrences of an
18
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
20
- L < B < C < Mix > > |Mix> or L < B < C < MixHash > > |MixHash> types (for arbitrary
21
- numbers).
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).
22
21
23
22
= begin comment
24
23
= defn Bag or BagHash
@@ -35,15 +34,15 @@ TODO: Partial type graph showing only QuantHash, Setty, Baggy, Mixy, Set,
35
34
The six collection classes C < Set > , C < SetHash > , C < Bag > , C < BagHash > , C < Mix > ,
36
35
C < MixHash > , all share similar semantics.
37
36
38
- For one thing, as far as they are concerned, identical objects refer to the same
39
- element -- where identity is determined using the L < WHICH > methods (i.e. the same
40
- way that the L < === > operator checks identity). For value types like C < Str > , this
41
- means having the same value; for reference types like C < Array > , it means
42
- referring to the same object instance.
37
+ For one thing, as far as they are concerned, identical objects refer to the
38
+ same element -- where identity is determined using the L < WHICH > methods
39
+ (i.e. the same way that the L < === > operator checks identity). For value
40
+ types like C < Str > , this means having the same value; for reference types
41
+ like C < Array > , it means referring to the same object instance.
43
42
44
- Secondly, they provide a Hash-like interface where the actual elements of the
45
- collection (which can be objects of any type) are the 'keys', and the associated
46
- weights are the 'values':
43
+ Secondly, they provide a Hash-like interface where the actual elements of
44
+ the collection (which can be objects of any type) are the 'keys', and the
45
+ associated weights are the 'values':
47
46
48
47
=table
49
48
value of $a{$b} if $b value of $a{$b} if $b
@@ -58,23 +57,23 @@ weights are the 'values':
58
57
= comment TODO: Update this after ab5tract's set/bag/mix operator redesign.
59
58
60
59
There are many infixes devoted to preforming common operations on
61
- L < C < Set > s|Set> , such as unions and set differences. Other operations
62
- include boolean checks, like whether an object is an element of a
63
- C < Set > , or whether one C < Set > is a subset of another C < Set > .
60
+ L < C < Set > s|Set> , such as unions and set differences. Other operations include
61
+ boolean checks, like whether an object is an element of a C < Set > , or whether
62
+ one C < Set > is a subset of another C < Set > .
64
63
65
- These infixes can be written using the UTF-8 character that represents
66
- the function (like L < C < ∈ > |∈> , or L < C < ∪ > |∪> ), or they can be written with
67
- an equivalent ASCII version (like L < C < (elem) > |(elem)> or L < C < (|) > |(|)> ).
64
+ These infixes can be written using the UTF-8 character that represents the
65
+ function (like L < C < ∈ > |∈> , or L < C < ∪ > |∪> ), or they can be written with an
66
+ equivalent ASCII version (like L < C < (elem) > |(elem)> or L < C < (|) > |(|)> ).
68
67
69
68
Most of the time, explicitly using C < Set > objects with these infixes is
70
69
unnecessary. All of the infix operators will work on any objects of type
71
70
L < C < Any > |Any> for its arguments (e.g., L < C < List > s|List> ,
72
71
L < C < Parcel > s|Parcel> , L < C < Mix > es|Mix> , etc.) and coerce them to C < Set > s
73
72
where needed.
74
73
75
- In some cases, if the type of an argument is a L < Bag > , the
76
- infix operator will behave in a different but analogous way to the way
77
- it would behave with only C < Set > arguments.
74
+ In some cases, if the type of an argument is a L < Bag > , the infix operator
75
+ will behave in a different but analogous way to the way it would behave with
76
+ only C < Set > arguments.
78
77
79
78
= head2 Operators that return C < Bool >
80
79
@@ -95,8 +94,8 @@ Equivalent to L«(elem)».
95
94
96
95
only sub infix:<<"\x2209">>($a, $b --> Bool)
97
96
98
- Equivalent to C < !(elem) > , i.e., returns C < True > if C < $a > is not an
99
- element of C < $b > .
97
+ Equivalent to C < !(elem) > , i.e., returns C < True > if C < $a > is not an element
98
+ of C < $b > .
100
99
101
100
= head3 infix (cont)
102
101
@@ -115,16 +114,16 @@ Equivalent to L«(cont)».
115
114
116
115
only sub infix:<<"\x220C">>($a, $b --> Bool)
117
116
118
- Equivalent to C < !(cont) > , i.e., returns C < True > if C < $a > does
119
- not contain C < $b > .
117
+ Equivalent to C < !(cont) > , i.e., returns C < True > if C < $a > does not contain
118
+ C < $b > .
120
119
121
120
= head3 infix (<=)
122
121
123
122
multi sub infix:<<(<=)>>(Any $a, Any $b --> Bool)
124
123
multi sub infix:<<(<=)>>(Setty $a, Setty $b --> Bool)
125
124
126
- Returns C < True > if C < $a > is a B < subset > or is equal to C < $b > , i.e., if all the
127
- elements of C < $a > are elements of C < $b > and C < $a > is a smaller or equal
125
+ Returns C < True > if C < $a > is a B < subset > or is equal to C < $b > , i.e., if all
126
+ the elements of C < $a > are elements of C < $b > and C < $a > is a smaller or equal
128
127
sized set than C < $b > .
129
128
130
129
= head4 infix ⊆
@@ -165,8 +164,8 @@ Equivalent to C«!(<)».
165
164
multi sub infix:<<(>=)>>(Any $a, Any $b --> Bool)
166
165
multi sub infix:<<(>=)>>(Setty $a, Setty $b --> Bool)
167
166
168
- Like L « (<=) » with reversed arguments. Returns C < True > if C < $a >
169
- is a B < superset > of or equal to C < $b > .
167
+ Like L « (<=) » with reversed arguments. Returns C < True > if C < $a > is a
168
+ B < superset > of or equal to C < $b > .
170
169
171
170
= head4 infix ⊇
172
171
@@ -185,8 +184,8 @@ Equivalent to C«!(>=)».
185
184
multi sub infix:<<(>)>>(Any $a, Any $b --> Bool)
186
185
multi sub infix:<<(>)>>(Setty $a, Setty $b --> Bool)
187
186
188
- Like L « (<) » with reversed arguments. Returns C < True > if C < $a >
189
- is a B < strict superset > of C < $b > .
187
+ Like L « (<) » with reversed arguments. Returns C < True > if C < $a > is a
188
+ B < strict superset > of C < $b > .
190
189
191
190
= head4 infix ⊃
192
191
@@ -221,8 +220,8 @@ Equivalent to L«(<+)».
221
220
multi sub infix:<<(>+)>>(Any $a, Any $b --> Bool)
222
221
223
222
Returns C < True > if C < $a > is a Baggy B < superset > of C < $b > , i.e., if all the
224
- elements of C < $b > are in C < $a > and no argument of C < $a > is weighted
225
- heavier than that element is in C < $b > .
223
+ elements of C < $b > are in C < $a > and no argument of C < $a > is weighted heavier
224
+ than that element is in C < $b > .
226
225
227
226
= head4 infix ≽
228
227
@@ -241,9 +240,9 @@ C<Set> that contains all the elements its arguments contain.
241
240
242
241
<a a b c d> (|) <h g f e d c> (|) <i j> === set <a b c d e f g h i j>
243
242
244
- If any of its arguments are C < Baggy > , it creates a new C < Bag > that
245
- contains all the elements of the arguments, each weighed by the highest
246
- weight that appeared for that element.
243
+ If any of its arguments are C < Baggy > , it creates a new C < Bag > that contains
244
+ all the elements of the arguments, each weighed by the highest weight that
245
+ appeared for that element.
247
246
248
247
bag(<a a b c a>) (|) bag(<a a b c c>) === bag(<a a a b c c>)
249
248
@@ -257,16 +256,16 @@ Equivalent to L«(V<|>)».
257
256
258
257
only sub infix:<(&)>(**@p)
259
258
260
- Returns the B < intersection > of all of its arguments. Generally, this
261
- creates a new C < Set > that contains only the elements that all of its
262
- arguments contain.
259
+ Returns the B < intersection > of all of its arguments. Generally, this creates
260
+ a new C < Set > that contains only the elements that all of its arguments
261
+ contain.
263
262
264
263
<a b c> (&) <b c d> === set <b c>
265
264
<a b c d> (&) <b c d e> (&) <c d e f> === set <c d>
266
265
267
266
If any of its arguments are C < Baggy > , this creates a new C < Bag > that
268
- contains only the elements that all of the arguments contain, each
269
- weighted by the maximum weight all of the arguments share for that element.
267
+ contains only the elements that all of the arguments contain, each weighted
268
+ by the maximum weight all of the arguments share for that element.
270
269
271
270
bag(<a a b c a>) (&) bag(<a a b c c>) === bag(<a a b c>)
272
271
@@ -280,14 +279,14 @@ Equivalent to L«(&)».
280
279
281
280
only sub infix:<(-)>(**@p)
282
281
283
- Returns the B < set difference > of all its arguments. Generally, this
284
- returns the C < Set > made up of all the elements the first argument has but
285
- the rest don't, i.e., of all the elements of the first argument, minus
286
- the elements from the other arguments.
282
+ Returns the B < set difference > of all its arguments. Generally, this returns
283
+ the C < Set > made up of all the elements the first argument has but the rest
284
+ don't, i.e., of all the elements of the first argument, minus the elements
285
+ from the other arguments.
287
286
288
287
If the first argument is C < Baggy > , this returns a C < Bag > that contains each
289
- element of the first argument with its weight subtracted by the weight
290
- of that element in each of the other arguments.
288
+ element of the first argument with its weight subtracted by the weight of
289
+ that element in each of the other arguments.
291
290
292
291
bag(<a a b c a d>) (-) bag(<a a b c c>) = bag(<a d>)
293
292
bag(<a a a a c d d d>) (-) bag(<a b d a>) (-) bag(<d c>) = bag(<a a d d>)
@@ -303,10 +302,10 @@ Equivalent to L«(-)».
303
302
multi sub infix:<(^)>(Any $a, Any $b --> Setty)
304
303
multi sub infix:<(^)>(Set $a, Set $b --> Setty)
305
304
306
- Returns the B < symmetric set difference > of all its arguments, i.e., a
307
- C < Set > made up of all the elements that C < $a > has but C < $b > doesn't and
308
- all the elements C < $b > has but C < $a > doesn't. Equivalent to C < ($a ∖ $b)
309
- ∪ ($b ∖ $a)> .
305
+ Returns the B < symmetric set difference > of all its arguments, i.e., a C < Set >
306
+ made up of all the elements that C < $a > has but C < $b > doesn't and all the
307
+ elements C < $b > has but C < $a > doesn't. Equivalent to C < ($a ∖ $b) ∪ ($b ∖
308
+ $a) > .
310
309
311
310
= head4 infix ⊖
312
311
@@ -335,9 +334,9 @@ Equivalent to L«(.)».
335
334
336
335
only sub infix:<(+)>(**@p)
337
336
338
- Returns the Baggy B < addition > of its arguments, i.e., a C < Bag > that
339
- contains each element of the arguments with the weights of the element
340
- across the arguments added together to get the new weight.
337
+ Returns the Baggy B < addition > of its arguments, i.e., a C < Bag > that contains
338
+ each element of the arguments with the weights of the element across the
339
+ arguments added together to get the new weight.
341
340
342
341
bag(<a a b c a d>) (.) bag(<a a b c c>) === bag(<a a a a a b b c c c d>)
343
342
0 commit comments