/
setbagmix.pod6
501 lines (313 loc) · 15.1 KB
/
setbagmix.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
=begin pod :tag<perl6>
=TITLE Sets, Bags, and Mixes
=SUBTITLE Unordered collections of unique and weighted objects in Perl 6
Often you want to collect objects in a container but you do not care
about the order of these objects. For such cases, Perl 6 provides the
I<unordered> collection types L<B<C<Set>>|Set>,
L<B<C<SetHash>>|SetHash>, L<B<C<Bag>>|Bag>, L<B<C<BagHash>>|BagHash>,
L<B<C<Mix>>|Mix>, and L<B<C<MixHash>>|MixHash>. Being unordered, these
containers can be more efficient than L<B<C<Lists>>|List> for looking up
elements or dealing with repeated items.
If you want to get the contained objects (elements) without duplicates
and you only care I<whether> an element is in the collection or not,
you can use a L<B<C<Set>>|Set> or L<B<C<SetHash>>|SetHash>. (If you want
to get rid of duplicates but still preserve order, take a look at the
L<C<unique>|unique> routine for L<B<C<Lists>>|List>.)
=begin comment
=defn Set or SetHash
Collection of distinct objects
=end comment
If you want to keep track of the number of times each object appeared,
you can use a L<B<C<Bag>>|Bag> or L<B<C<BagHash>>|BagHash>. In these
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 L<B<C<Mix>>|Mix> and L<B<C<MixHash>>|MixHash> are
similar, but they also allow fractional weights.
=begin comment
=defn Bag or BagHash
Collection of distinct objects mapped to integer weights
=defn Mix or MixHash
Collection of distinct objects mapped to real-number weights
=end comment
=for comment
TODO: Partial type graph showing only QuantHash, Setty, Baggy, Mixy, Set,
SetHash, Bag, BagHash, Mix, MixHash.
L<B<C<Set>>|Set>, L<B<C<Bag>>|Bag>, and L<B<C<Mix>>|Mix> are I<immutable>
types. Use the mutable variants L<B<C<SetHash>>|SetHash>, L<B<C<BagHash>>|BagHash>,
and L<B<C<MixHash>>|MixHash> if you want to add or remove elements after
the container has been constructed.
The six collection classes C<Set>, C<SetHash>, C<Bag>, C<BagHash>, C<Mix>,
C<MixHash>, all share similar semantics.
For one thing, as far as they are concerned, identical objects refer to the
same element – where identity is determined using the L<WHICH|/routine/WHICH> method
(i.e. the same way that the L<===> operator checks identity). For value
types like C<Str>, this means having the same value; for reference types
like C<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':
=table
value of $a{$b} if $b value of $a{$b} if $b
type of $a is an element is not an element
------------- ---------------------- ---------------------
Set / SetHash True False
Bag / BagHash a positive integer 0
Mix / MixHash a non-zero real number 0
=head1 Set/Bag Operators
=comment TODO: Update this after ab5tract's set/bag/mix operator redesign.
There are many infixes devoted to performing common operations on
L<C<Set>s|Set>, such as unions and set differences. Other operations 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>.
These infixes can be written using the UTF-8 character that represents the
function (like L<C<∈>|∈>, or L<C<∪>|∪>), or they can be written with an
equivalent ASCII version (like L<C<(elem)>|/routine/(elem)> or L<C<(|)>|(|)>).
Most of the time, explicitly using C<Set> objects with these infixes is
unnecessary. All of the infix operators will work on any objects of type
L<C<Any>|Any> for its arguments (e.g., L<C<List>s|List>,
L<C<Array>s|Array>, L<C<Mix>es|Mix>, etc.) and coerce them to C<Set>s
where needed.
In some cases, if the type of an argument is a L<Bag>, the infix operator
will behave in a different but analogous way to the way it would behave with
only C<Set> arguments.
=head2 Operators that return C<Bool>
=head3 infix (elem)
multi infix:<(elem)>($a, Any $b --> Bool)
multi infix:<(elem)>($a, Set $b --> Bool)
X<Membership operator>.
Returns C<True> if C<$a> is an B<element> of C<$b>.
say 2 (elem) (1, 2, 3).Set; # OUTPUT: «True»
say 4 (elem) (1, 2, 3).Set; # OUTPUT: «False»
=head4 infix ∈
only sub infix:<∈>($a, $b --> Bool)
X<Membership operator> (alternate).
Equivalent to L«(elem)|/routine/(elem)», at codepoint U+2208 (ELEMENT OF).
=head4 infix ∉
only sub infix:<∉>($a, $b --> Bool)
X<Non-membership operator>.
Equivalent to C<!(elem)>, i.e., returns C<True> if C<$a> is not an element
of C<$b>, at codepoint U+2209 (NOT AN ELEMENT OF).
say 2 !(elem) (1, 2, 3).Set; # OUTPUT: «False»
say 4 !(elem) (1, 2, 3).Set; # OUTPUT: «True»
=head3 infix (cont)
multi infix:<(cont)>(Any $a, $b --> Bool)
multi infix:<(cont)>(Set $a, $b --> Bool)
X<Contains operator>.
Returns C<True> if C<$a> B<contains> C<$b> as an element.
say (1, 2, 3).Set (cont) 2; # OUTPUT: «True»
say (1, 2, 3).Set (cont) 4; # OUTPUT: «False»
=head4 infix ∋
only sub infix:<∋>($a, $b --> Bool)
X<Contains operator> (alternate).
Equivalent to L«(cont)|/routine/(cont)», at codepoint U+220B (CONTAINS AS MEMBER).
=head4 infix ∌
only sub infix:<∌>($a, $b --> Bool)
X<Does not contain operator>.
Equivalent to C<!(cont)>, i.e., returns C<True> if C<$a> does not contain
C<$b>, at codepoint U+220C (DOES NOT CONTAIN AS MEMBER).
say (1, 2, 3).Set !(cont) 2; # OUTPUT: «False»
say (1, 2, 3).Set !(cont) 4; # OUTPUT: «True»
=head3 infix (<=)
multi infix:<<(<=)>>(Any $a, Any $b --> Bool)
multi infix:<<(<=)>>(Setty $a, Setty $b --> Bool)
X<Subset of or equal to operator>.
Returns C<True> if C<$a> is a B<subset> or is equal to C<$b>, i.e., if all
the elements of C<$a> are elements of C<$b> and C<$a> is a smaller or equal
sized set than C<$b>.
say (1, 2, 3).Set (<=) (3, 2, 1).Set; # OUTPUT: «True»
say (1, 3).Set (<=) (2, 1).Set; # OUTPUT: «False»
say ∅ (<=) (3, 2, 1).Set; # OUTPUT: «True»
=head4 infix ⊆
only sub infix:<⊆>($a, $b --> Bool)
X<Subset of or equal to operator> (alternate).
Equivalent to L«(<=)», at codepoint U+2286 (SUBSET OF OR EQUAL TO).
=head4 infix ⊈
only sub infix:<⊈>($a, $b --> Bool)
X<Neither subset of nor equal to operator>.
Equivalent to C«!(<=)», at codepoint U+2288 (NEITHER A SUBSET OF NOR EQUAL TO).
say (1, 2, 3).Set !(<=) (3, 2, 1).Set; # OUTPUT: «False»
say (1, 3).Set ⊈ (2, 1).Set; # OUTPUT: «True»
=head3 infix (<)
multi infix:<<(<)>>(Any $a, Any $b --> Bool)
multi infix:<<(<)>>(Setty $a, Setty $b --> Bool)
X<Subset of operator>.
Returns C<True> if C<$a> is a B<strict subset> of C<$b>, i.e., that all the
elements of C<$a> are elements of C<$b> but C<$a> is a smaller set than
C<$b>.
say (1, 2, 3).Set (<) (3, 2, 1).Set; # OUTPUT: «False»
say (1, 3).Set (<) (3, 2, 1).Set; # OUTPUT: «True»
say ∅ (<) (3, 2, 1).Set; # OUTPUT: «True»
=head4 infix ⊂
only sub infix:<⊂>($a, $b --> Bool)
X<Subset of operator> (alternate).
Equivalent to L«(<)», at codepoint U+2282 (SUBSET OF).
=head4 infix ⊄
only sub infix:<⊄>($a, $b --> Bool)
X<Not a subset of operator>.
Equivalent to C«!(<)», at codepoint U+2284 (NOT A SUBSET OF).
say (1, 2, 3).Set !(<) (3, 2, 1).Set; # OUTPUT: «True»
say (1, 3).Set ⊄ (3, 2, 1).Set; # OUTPUT: «False»
=head3 infix (>=)
multi infix:<<(>=)>>(Any $a, Any $b --> Bool)
multi infix:<<(>=)>>(Setty $a, Setty $b --> Bool)
X<Superset of or equal to operator>.
Like L«(<=)» with reversed arguments. Returns C<True> if C<$a> is a
B<superset> of or equal to C<$b>.
say (1, 2, 3).Set (>=) (3, 2, 1).Set; # OUTPUT: «True»
say (1, 3).Set (>=) (3, 2, 1).Set; # OUTPUT: «False»
say ∅ (>=) (3, 2, 1).Set; # OUTPUT: «False»
=head4 infix ⊇
only sub infix:<⊇>($a, $b --> Bool)
X<Superset of or equal to operator> (alternate).
Equivalent to L«(>=)», at codepoint U+2287 (SUPERSET OF OR EQUAL TO).
=head4 infix ⊉
only sub infix:<⊉>($a, $b --> Bool)
X<Neither a superset of nor equal to operator>.
Equivalent to C«!(>=)», at codepoint U+2289 (NEITHER A SUPERSET OF
NOR EQUAL TO).
say (1, 2, 3).Set !(>=) (3, 2, 1).Set; # OUTPUT: «False»
say (1, 3).Set ⊉ (3, 2, 1).Set; # OUTPUT: «True»
=head3 infix (>)
multi infix:<<(>)>>(Any $a, Any $b --> Bool)
multi infix:<<(>)>>(Setty $a, Setty $b --> Bool)
X<Superset of operator>.
Like L«(<)» with reversed arguments. Returns C<True> if C<$a> is a
B<strict superset> of C<$b>.
say (1, 2, 3, 4).Set (>) (3, 2, 1).Set; # OUTPUT: «True»
say (1, 3).Set (>) (3, 2, 1).Set; # OUTPUT: «False»
say ∅ (>) (3, 2, 1).Set; # OUTPUT: «False»
=head4 infix ⊃
only sub infix:<⊃>($a, $b --> Bool)
X<Superset of operator> (alternate).
Equivalent to L«(>)», at codepoint U+2283 (SUPERSET OF).
=head4 infix ⊅
only sub infix:<⊅>($a, $b --> Bool)
X<Not a superset of operator>.
Equivalent to C«!(>)», at codepoint U+2285 (NOT A SUPERSET OF).
say (1, 2, 3, 4).Set !(>) (3, 2, 1).Set; # OUTPUT: «False»
say (1, 3).Set ⊅ (3, 2, 1).Set; # OUTPUT: «True»
=head3 infix (<+)
multi infix:<<(<+)>>(Any $a, Any $b --> Bool)
multi infix:<<(<+)>>(Baggy $a, Baggy $b --> Bool)
X<Baggy subset of operator>.
Returns C<True> if C<$a> is a Baggy B<subset> of C<$b>, i.e., if all the
elements of C<$a> are in C<$b> and each element of C<$b> is weighed at
least as heavily as the element is in C<$a>.
say (1, 2, 3).Bag (<+) (3, 2, 1).Bag; # OUTPUT: «True»
say (1, 2, 2, 3).Bag (<+) (3, 2, 1).Bag; # OUTPUT: «False»
=head4 infix ≼
only sub infix:<≼>($a, $b --> Bool)
X<Baggy subset of operator> (alternate).
Equivalent to L«(<+)», at codepoint U+227C (PRECEDES OR EQUAL TO).
=head3 infix (>+)
multi infix:<<(>+)>>(Baggy $a, Baggy $b --> Bool)
multi infix:<<(>+)>>(Any $a, Any $b --> Bool)
X<Baggy superset of operator>.
Returns C<True> if C<$a> is a Baggy B<superset> of C<$b>, i.e., if all the
elements of C<$b> are in C<$a> and no element of C<$b> is weighted heavier
than that element is in C<$a>.
say (1, 2, 3).Bag (>+) (3, 2, 1).Bag; # OUTPUT: «True»
say (1, 2, 2, 3).Bag (>+) (3, 2, 1).Bag; # OUTPUT: «True»
say (1, 2).Bag (>+) (3, 2, 1).Bag; # OUTPUT: «False»
=head4 infix ≽
only sub infix:<≽>($a, $b --> Bool)
X<Baggy superset of operator> (alternate).
Equivalent to L«(>+)», at codepoint U+227D (SUCCEEDS OR EQUAL TO).
=head2 Operators that return C<Set> or C<Bag>
=head3 infix (|)
only sub infix:<(|)>(**@p)
X<Union operator>.
Returns the B<union> of all its arguments. Generally, this creates a new
C<Set> that contains all the elements its arguments contain.
=begin code
<a a b c d> (|) <h g f e d c> (|) <i j> === set <a b c d e f g h i j>
=end code
If any of its arguments are C<Baggy>, it creates a new C<Bag> that contains
all the elements of the arguments, each weighed by the highest weight that
appeared for that element.
=begin code
bag(<a a b c a>) (|) bag(<a a b c c>) === bag(<a a a b c c>)
=end code
=head4 infix ∪
only sub infix:<∪>(|p)
X<Union operator> (alternate).
Equivalent to L«(V<|>)», at codepoint U+222A (UNION).
=head3 infix (&)
only sub infix:<(&)>(**@p)
X<Intersection operator>.
Returns the B<intersection> of all of its arguments. Generally, this creates
a new C<Set> that contains only the elements common to all of the arguments.
=begin code :skip-test
<a b c> (&) <b c d> === set <b c>
<a b c d> (&) <b c d e> (&) <c d e f> === set <c d>
=end code
If any of the arguments are C<Baggy>, the result is a new C<Bag> containing
the common elements, each weighted by the largest I<common> weight (which
is the minimum of the weights of that element over all arguments).
=begin code
bag(<a a b c a>) (&) bag(<a a b c c>) === bag(<a a b c>)
=end code
=head4 infix ∩
only sub infix:<∩>(|p)
X<Intersection operator> (alternate).
Equivalent to L«(&)», at codepoint U+2229 (INTERSECTION).
=head3 infix (-)
only sub infix:<(-)>(**@p)
X<Set difference operator>.
Returns the B<set difference> of all its arguments. Generally, this returns
the C<Set> made up of all the elements the first argument has but the rest
don't, i.e., of all the elements of the first argument, minus the elements
from the other arguments.
If the first argument is C<Baggy>, this returns a C<Bag> that contains each
element of the first argument with its weight subtracted by the weight of
that element in each of the other arguments.
=begin code :skip-test
bag(<a a b c a d>) (-) bag(<a a b c c>) === bag(<a d>)
bag(<a a a a c d d d>) (-) bag(<a b d a>) (-) bag(<d c>) === bag(<a a d>)
=end code
=head4 infix ∖
only sub infix:<<"\x2216">>(|p)
X<Set difference operator> (alternate).
Equivalent to L«(-)».
=head3 infix (^)
multi infix:<(^)>(Any $a, Any $b --> Setty)
multi infix:<(^)>(Set $a, Set $b --> Setty)
X<Symmetric set difference operator>.
Returns the B<symmetric set difference> of all its arguments, i.e., a C<Set>
made up of all the elements that C<$a> has but C<$b> doesn't and all the
elements C<$b> has but C<$a> doesn't. Equivalent to C<($a ∖ $b) ∪ ($b ∖
$a)>.
=head4 infix ⊖
only sub infix:<⊖>($a, $b --> Setty)
X<Symmetric set difference operator> (alternate).
Equivalent to L«(^)», at codepoint U+2296 (CIRCLED MINUS).
=head3 infix (.)
only sub infix:<(.)>(**@p)
X<Baggy multiplication operator>.
Returns the Baggy B<multiplication> of its arguments, i.e., a C<Bag> that
contains each element of the arguments with the weights of the element
across the arguments multiplied together to get the new weight.
=begin code :skip-test
<a b c> (.) <a b c d> === bag <a b c> # Since 1 * 0 == 0, in the case of 'd'
bag(<a a b c a d>) (.) bag(<a a b c c>) === ("a"=>6,"c"=>2,"b"=>1).Bag
=end code
=head4 infix ⊍
only sub infix:<⊍>(|p)
X<Baggy multiplication operator> (alternate).
Equivalent to L«infix (.)|(.)», at codepoint U+228D (MULTISET MULTIPLICATION).
=head3 infix (+)
only sub infix:<(+)>(**@p)
X<Baggy addition operator>.
Returns the Baggy B<addition> of its arguments, i.e., a C<Bag> that contains
each element of the arguments with the weights of the element across the
arguments added together to get the new weight.
=begin code
bag(<a a b c a d>) (+) bag(<a a b c c>) === ("a"=>5,"c"=>3,"b"=>2,"d"=>1).Bag
=end code
=head4 infix ⊎
only sub infix:<⊎>(|p)
X<Baggy addition operator> (alternate).
Equivalent to L«(+)», at codepoint U+228E (MULTISET UNION).
=head3 term ∅
Equivalent to set(), aka X<the empty set>, at codepoint U+2205 (EMPTY SET).
=end pod