Set

Inherits from: Object
Instance variables: dict

An unordered collection of unique elements backed by a Dictionary.

Elements can be any object that responds to hash and =. Duplicate additions are silently ignored. Use add:, remove:, includes: for basic operations and do:, select:, reject: for iteration.

Core operations (new, add:, remove:, includes:, size, isEmpty, do:) are implemented as VM primitives. This file adds convenience methods on top.

Test
Set new size >>> 0
Set new isEmpty >>> true
Example
"Build a set and check membership"
| s <Set> |
s := Set new.
s add: 1.
s add: 2.
s add: 1.
s size

Class Methods

primitives

class new

Create a new empty set.

Instance Methods

primitives

add:

Add element to the set. Returns self.

do:

Evaluate block once for each element.

includes:

Return true if the set contains element.

isEmpty

Return true if the set is empty.

remove:

Remove element from the set.

size

Return the number of elements.

uncategorized

collect:

Return a new Array with block applied to each element.

Test
s := Set new. s add: 1. s add: 2.
(s collect: [:x | x * 10]) size >>> 2
printString

Return a human-readable string representation.

reject:

Return a new Set excluding elements for which block returns true.

Test
s := Set new. s add: 1. s add: 2. s add: 3. s add: 4.
odds := s reject: [:x | x even].
odds size >>> 2
odds includes: 1 >>> true
odds includes: 3 >>> true
select:

Return a new Set containing only elements for which block returns true.

Test
s := Set new. s add: 1. s add: 2. s add: 3. s add: 4.
evens := s select: [:x | x even].
evens size >>> 2
evens includes: 2 >>> true
evens includes: 4 >>> true