polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Collections / Set.html
blob3177c04d8d566273ab60f8cdb3314d3f6d07386c
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0021e7}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Helvetica; min-height: 16.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #000000; min-height: 14.0px}
24 span.s1 {font: 18.0px Helvetica}
25 span.s2 {color: #001bb9}
26 span.s3 {color: #0000ff}
27 span.s4 {color: #000000}
28 span.s5 {text-decoration: underline}
29 span.s6 {color: #606060}
30 span.s7 {color: #0000bf}
31 span.s8 {color: #bf0000}
32 span.s9 {color: #007300}
33 span.Apple-tab-span {white-space:pre}
34 </style>
35 </head>
36 <body>
37 <p class="p1"><span class="s1"><b>Set<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b></span><b>a set according to equality</b></p>
38 <p class="p2"><br></p>
39 <p class="p1"><b>Inherits from: </b><a href="../Core/Object.html"><span class="s2"><b>Object</b></span></a><b> : </b><a href="Collection.html"><span class="s2"><b>Collection</b></span></a><span class="s2"><b><span class="Apple-converted-space"> </span></b></span></p>
40 <p class="p2"><br></p>
41 <p class="p1">A Set is s collection of objects, no two of which are equal.</p>
42 <p class="p1">Most of its methods are inherited from Collection.</p>
43 <p class="p1">The contents of a Set are unordered. You must not depend on the order of items in a set.</p>
44 <p class="p1">For an ordered set, see <a href="OrderedIdentitySet.html"><span class="s3">OrderedIdentitySet</span></a>.</p>
45 <p class="p2"><br></p>
46 <p class="p3"><span class="s4">See also: <a href="IdentitySet.html"><span class="s5">IdentitySet</span></a>, <a href="List.html"><span class="s5">List</span></a>, <a href="Dictionary.html"><span class="s5">Dictionary</span></a></span></p>
47 <p class="p2"><br></p>
48 <p class="p4"><b></b><br></p>
49 <p class="p5"><b>Adding and Removing:</b></p>
50 <p class="p2"><br></p>
51 <p class="p1"><b>add(anObject)</b></p>
52 <p class="p2"><br></p>
53 <p class="p1">Add anObject to the Set. An object which is equal to an object already in the Set will not be added.</p>
54 <p class="p6"><br></p>
55 <p class="p7"><span class="s2">Set</span>[1, 2, 3].add(4).postln;</p>
56 <p class="p8"><br></p>
57 <p class="p7"><span class="s2">Set</span>[1, 2, 3].add(3).postln;</p>
58 <p class="p8"><br></p>
59 <p class="p7"><span class="s2">Set</span>[<span class="s6">"abc"</span>, <span class="s6">"def"</span>, <span class="s6">"ghi"</span>].add(<span class="s6">"jkl"</span>).postln;</p>
60 <p class="p8"><br></p>
61 <p class="p9"><span class="s2">Set</span><span class="s4">[</span>"abc"<span class="s4">, </span>"def"<span class="s4">, </span>"ghi"<span class="s4">].add(</span>"def"<span class="s4">).postln;</span></p>
62 <p class="p2"><br></p>
63 <p class="p1"><b>remove(anObject)</b></p>
64 <p class="p2"><br></p>
65 <p class="p1">Remove anObject from the Set.</p>
66 <p class="p2"><br></p>
67 <p class="p7"><span class="s2">Set</span>[1, 2, 3].remove(3).postln;</p>
68 <p class="p6"><br></p>
69 <p class="p10"><br></p>
70 <p class="p5"><b>Iteration:</b></p>
71 <p class="p2"><br></p>
72 <p class="p1"><b>do(function)</b></p>
73 <p class="p2"><br></p>
74 <p class="p1">Evaluates function for each item in the Set.</p>
75 <p class="p1">The function is passed two arguments, the item and an integer index.</p>
76 <p class="p2"><br></p>
77 <p class="p7"><span class="s2">Set</span>[1, 2, 3, 300].do({ <span class="s2">arg</span> item, i; item.postln });</p>
78 <p class="p8"><br></p>
79 <p class="p1"><b>keyAt(index)</b></p>
80 <p class="p2"><br></p>
81 <p class="p1">returns the object at the internal <b>index</b>. This index is not deterministic.</p>
82 <p class="p2"><br></p>
83 <p class="p2"><br></p>
84 <p class="p2"><br></p>
85 <p class="p5"><b>Set specific operations:</b></p>
86 <p class="p4"><b></b><br></p>
87 <p class="p2"><br></p>
88 <p class="p1"><b>sect(that)</b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>return the set theoretical intersection of <b>this</b> and <b>that</b></p>
89 <p class="p1"><b>this &amp; that</b></p>
90 <p class="p8"><br></p>
91 <p class="p7">a = <span class="s7">Set</span>[1, 2, 3]; b = <span class="s7">Set</span>[2, 3, 4, 5];</p>
92 <p class="p7">sect(a, b);</p>
93 <p class="p11"><span class="s4">a &amp; b </span>// shorter syntax</p>
94 <p class="p2"><br></p>
95 <p class="p1"><b>union(that)</b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>return the set theoretical union of <b>this</b> and <b>that</b></p>
96 <p class="p1"><b>this | that</b></p>
97 <p class="p12"><br></p>
98 <p class="p13">a = <span class="s7">Set</span>[1, 2, 3]; b = <span class="s7">Set</span>[2, 3, 4, 5];</p>
99 <p class="p13">union(a, b);</p>
100 <p class="p11"><span class="s4">a | b </span>// shorter syntax</p>
101 <p class="p2"><br></p>
102 <p class="p1"><b>difference(that)<span class="Apple-tab-span"> </span></b><span class="Apple-tab-span"> </span>return the set of all items which are elements of <b>this</b>, but not of <b>that</b></p>
103 <p class="p1"><b>this - that</b></p>
104 <p class="p8"><br></p>
105 <p class="p7">a = <span class="s7">Set</span>[1, 2, 3]; b = <span class="s7">Set</span>[2, 3, 4, 5];</p>
106 <p class="p7">difference(a, b);</p>
107 <p class="p11"><span class="s4">a - b </span>// shorter syntax</p>
108 <p class="p2"><br></p>
109 <p class="p1"><b>symmetricDifference(that)</b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>return the set of all items which are not elements of both<span class="Apple-converted-space">  </span><b>this</b> and <b>that</b></p>
110 <p class="p1"><b>this -- that</b></p>
111 <p class="p8"><br></p>
112 <p class="p7">a = <span class="s7">Set</span>[1, 2, 3]; b = <span class="s7">Set</span>[2, 3, 4, 5];</p>
113 <p class="p7">symmetricDifference(a, b);</p>
114 <p class="p11"><span class="s4">a -- b </span>// shorter syntax</p>
115 <p class="p2"><br></p>
116 <p class="p1"><b>isSubsetOf(that)</b><span class="Apple-tab-span"> </span>returns true if all elements of <b>this</b> are also elements of <b>that</b><span class="Apple-converted-space"> </span></p>
117 <p class="p2"><br></p>
118 <p class="p13">a = <span class="s7">Set</span>[1, 2, 3, 4];</p>
119 <p class="p13"><span class="s7">Set</span>[1, 2].isSubsetOf(a); <span class="s8">// true</span></p>
120 <p class="p13"><span class="s7">Set</span>[1, 5].isSubsetOf(a); <span class="s8">// false</span></p>
121 <p class="p2"><br></p>
122 <p class="p2"><br></p>
123 <p class="p14"><br></p>
124 <p class="p11">// example:</p>
125 <p class="p12"><br></p>
126 <p class="p13">a = <span class="s7">Set</span>[1, 2, 3, 4];</p>
127 <p class="p11"><span class="s4">b = a.powerset; </span>// set of all parts</p>
128 <p class="p11"><span class="s4">a.isSubsetOf(b); </span>// false: no set is ever part of itself.</p>
129 <p class="p11"><span class="s4">b.asArray.reduce(</span><span class="s9">\union</span><span class="s4">) == a; </span>// true parts may not contain other elements that original</p>
130 <p class="p13">b.asArray.reduce(<span class="s9">\difference</span>).isEmpty; <span class="s8">// true.</span></p>
131 <p class="p12"><br></p>
132 <p class="p11">// you can use Set to efficiently remove duplicates from an array:</p>
133 <p class="p12"><br></p>
134 <p class="p13">a = [1, 2, 3, 4, 3, 5, 5, 2, 2, 1];</p>
135 <p class="p11"><span class="s4">a.as(</span><span class="s7">Set</span><span class="s4">);<span class="Apple-tab-span"> </span></span>// convert to set</p>
136 <p class="p11"><span class="s4">a.as(</span><span class="s7">Set</span><span class="s4">).as(</span><span class="s7">Array</span><span class="s4">); </span>// and convert back</p>
137 <p class="p14"><br></p>
138 <p class="p14"><br></p>
139 </body>
140 </html>