Classes | Collections > Unordered

Set : Collection : Object

a set according to equality
Source: Set.sc
Subclasses: Dictionary, IdentitySet

Description

A Set is s collection of objects, no two of which are equal. Most of its methods are inherited from Collection. The contents of a Set are unordered. You must not depend on the order of items in a set. For an ordered set, see OrderedIdentitySet.

Class Methods

Inherited class methods

Undocumented class methods

Set.new(n: 2)

Instance Methods

Adding and Removing

.add(item)

Add anObject to the Set. An object which is equal to an object already in the Set will not be added.

Set[1, 2, 3].add(4).postln;
Set[1, 2, 3].add(3).postln;
Set["abc", "def", "ghi"].add("jkl").postln;
Set["abc", "def", "ghi"].add("def").postln;

.remove(item)

Remove anObject from the Set.

Set[1, 2, 3].remove(3).postln;

Iteration

.do(function)

Evaluates function for each item in the Set. The function is passed two arguments, the item and an integer index.

Set[1, 2, 3, 300].do({ arg item, i; item.postln });

.keyAt(index)

Returns the object at the internal index. This index is not deterministic.

Set specific operations

.sect(that)

&(that)

Return the set theoretical intersection of this and that.

a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
sect(a, b);
a & b // shorter syntax

.union(that)

|(that)

Return the set theoretical union of this and that.

a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
union(a, b);
a | b // shorter syntax

.difference(that)

-(that)

Return the set of all items which are elements of this, but not of that.

a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
difference(a, b);
a - b // shorter syntax

.symmetricDifference(that)

--(that)

Return the set of all items which are not elements of both this and that.

a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
symmetricDifference(a, b);
a -- b // shorter syntax

.isSubsetOf(that)

Returns true if all elements of this are also elements of that.

a = Set[1, 2, 3, 4];
Set[1, 2].isSubsetOf(a); // true
Set[1, 5].isSubsetOf(a); // false

Inherited instance methods

Undocumented instance methods

.array

.array = value

.asSet

.choose

.clear

.copy

.findMatch(item)

.fixCollisionsFrom(index)

.includes(item)

.makeEmpty

.pop

.scanFor(obj)

.size

.species

.unify

Examples

a = Set[1, 2, 3, 4];
b = a.powerset; // set of all parts
a.isSubsetOf(b); // false: no set is ever part of itself.
b.asArray.reduce(\union) == a; // true parts may not contain other elements that original
b.asArray.reduce(\difference).isEmpty; // true.
// you can use Set to efficiently remove duplicates from an array:

a = [1, 2, 3, 4, 3, 5, 5, 2, 2, 1];
a.as(Set);        // convert to set
a.as(Set).as(Array);    // and convert back