SuperCollider CLASSES


A Bag according to identity
Inherits from: Bag : Collection : Object


An IdentityBag is an unordered collection of objects. In some languages it is referred to as a counted set. A Bag keeps track of the number of times objects are inserted and requires that objects be removed the same number of times. There is only one instance of an object in a Bag even if the object has been added to the Bag multiple times (test is for identity).

The contents of a IdentityBag are unordered. You must not depend on the order of items in a set.

Class Methods

Inherited class methods

Instance Methods

Adding and Removing

-add (item, count: 1)

From superclass: Bag

Add anObject to the Bag. A Bag may contain multiple entries of the same object.

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

-remove (item, count: 1)

From superclass: Bag

Remove anObject from the IdentityBag.

IdentityBag[1, 2, 3].remove(3);


From superclass: Bag

Returns the dictionary that stores the objects in pairs (obj -> numberOfObjects)

IdentityBag[\a, \b, \c, \c].contents;


-do (function)

From superclass: Bag

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

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

Inherited instance methods

Difference between Bag and IdentityBag

// the two strings are equal, but not identical
"something" == "something"; // true
"something" === "something" // false

a =;
a.contents; // only one object in the bag really

a =;
a.contents; // two objects in the bag