Skip to content

set

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.

Fields

len

Returns the number of members.

Type: int

Methods

add value

Adds value if it is not already present.

Parameters:

Name Type Description
value the value to insert
let s = set [1, 2]
s.add 3
s.add 2
assert_eq [...s] [1, 2, 3]

delete value

Removes value if present.

Parameters:

Name Type Description
value the value to remove

Returns: bool indicating whether a value was removed

clear

Removes all members.

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

contains value

Tests whether the set contains value.

Parameters:

Name Type Description
value the value to test

Returns: bool

union other

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

Parameters:

Name Type Description
other set other set

Returns: set

intersect other

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

Parameters:

Name Type Description
other set other set

Returns: set

diff other

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

Parameters:

Name Type Description
other set other set

Returns: set

sym_diff other

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

Parameters:

Name Type Description
other set other set

Returns: set

is_subset other

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

Parameters:

Name Type Description
other set other set

Returns: bool

is_superset other

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

Parameters:

Name Type Description
other set other set

Returns: bool

Operations

Iteration

Iterating a set yields values in insertion order:

let s = set [3, 1, 2, 1]
assert_eq [...s] [3, 1, 2]