Skip to content

Set[T]

Sets are ordered, mutable collections with unique membership semantics.

Iteration preserves insertion order. Reinserting an existing value is a no-op and does not move it to the end.

Inherits from: Iterable[T]

Constructor

Set iterable

Builds a set from one iterable.

Parameters

NameTypeDescription
iterable Iterable[T]

Methods

(iter)()

Iterates values in insertion order:

let s = Set [3, 1, 2, 1]
assert_eq [...s] [3, 1, 2]
Returns an iterator.

add value

Adds value if it is not already present.

Parameters

NameTypeDescription
value T the value to insert

Example

let s = Set [1, 2]
s.add 3
s.add 2
assert_eq [...s] [1, 2, 3]

clear()

Removes all members.

let s = Set [1, 2]
s.clear()
assert_eq $s.len 0

contains value -> Bool

Tests whether the Set contains value.

Parameters

NameTypeDescription
value the value to test

copy() -> Set[T]

Returns a shallow copy of the Set.

Insertion order is preserved. Contained values are not recursively copied.

delete value -> Bool

Indicates whether a value was removed. Removes value if present.

Parameters

NameTypeDescription
value the value to remove

diff[U] other -> Set[T]

Returns a new Set containing members from the receiver that are not present in other.

Parameters

NameTypeDescription
other Set[U] other Set

intersect[U] other -> Set[T]

Returns a new Set containing members that are present in both sets, in the receiver's insertion order.

Parameters

NameTypeDescription
other Set[U] other Set

is_subset[U] other -> Bool

Returns true when every member of the receiver is present in other.

Parameters

NameTypeDescription
other Set[U] other Set

is_superset[U] other -> Bool

Returns true when every member of other is present in the receiver.

Parameters

NameTypeDescription
other Set[U] other Set

len() -> Int

Returns the number of members.

sym_diff[U] other -> Set[Union[T, U]]

Returns a new Set containing members present in exactly one of the two sets.

Parameters

NameTypeDescription
other Set[U] other Set

union[U] other -> Set[Union[T, U]]

Returns a new Set containing all members from self, then first-seen members from other.

Parameters

NameTypeDescription
other Set[U] other Set