x-lang

← Index

x/type/set

Set: mutable membership with O(1) expected add/has?/del, plus the set algebra, on a Dict.

Membership = key presence in the backing Dict; same key-type rules – content comparison, instances by identity.

Class Set

A mutable set with O(1) expected membership, backed by a Dict.

Elements follow Dict’s key rules: symbols, strings, integers, and chars compare by content (equal?); class instances are identity members (eq?).

Mutators (add!/del!) return the set for chaining; the algebra (union/intersection/difference) returns new sets and mutates neither operand.

d

Member: data carried by a Set instance.

(Set set? x)

Test whether a value is a Set.

Parameters:

Returns: BOOL — #t when x is a Set instance

(Set make)

An empty set.

Returns: Set — A new empty set

(Set from-list lst)

Build a set from a list’s elements.

Parameters:

Returns: Set — A set of the list’s distinct elements

Examples:

((Set from-list (list 1 2 2)) length) => 2

(Set of . args)

Variadic literal: a set of the arguments.

Parameters:

Returns: Set — A set of the distinct arguments

Examples:

((Set of 1 2 2 3) length) => 3

(%d)

Instance method: called on a Set instance.

(add! x)

Add an element (a no-op when already present); returns the set for chaining.

Instance method: called on a Set instance.

Parameters:

Returns: Set — self

(has? x)

Test membership.

Instance method: called on a Set instance.

Parameters:

Returns: BOOL — #t when x is a member

(del! x)

Remove an element (a no-op when absent); returns the set for chaining.

Instance method: called on a Set instance.

Parameters:

Returns: Set — self

(length)

The number of members (a stored property, O(1)).

Instance method: called on a Set instance.

Returns: INT — Member count

(empty?)

Test whether the set has no members.

Instance method: called on a Set instance.

Returns: BOOL — #t when empty

(copy)

A new set with the same members (a shallow copy).

Instance method: called on a Set instance.

Returns: Set — An independent set of the same members

(union other)

A new set of the members of either operand.

Instance method: called on a Set instance.

Parameters:

Returns: Set — The union; neither operand is mutated

Examples:

(((Set of 1 2) union (Set of 2 3)) length) => 3

(intersection other)

A new set of the members common to both operands.

Instance method: called on a Set instance.

Parameters:

Returns: Set — The intersection; neither operand is mutated

Examples:

(((Set of 1 2) intersection (Set of 2 3)) ->list) => (2)

(difference other)

A new set of this set’s members absent from other.

Instance method: called on a Set instance.

Parameters:

Returns: Set — The difference; neither operand is mutated

Examples:

(((Set of 1 2) difference (Set of 2 3)) ->list) => (1)

(subset? other)

Test whether every member of this set is in other.

Instance method: called on a Set instance.

Parameters:

Returns: BOOL — #t when this set is a subset of other

Examples:

((Set of 1 2) subset? (Set of 1 2 3)) => #t

(superset? other)

Test whether this set contains every member of other.

Instance method: called on a Set instance.

Parameters:

Returns: BOOL — #t when this set is a superset of other

(=? other)

Test whether both sets hold exactly the same members.

Instance method: called on a Set instance.

Parameters:

Returns: BOOL — #t when the sets are equal

Examples:

((Set of 1 2) =? (Set of 2 1)) => #t

(for-each f)

Apply f to each member, for side effects (unordered).

Instance method: called on a Set instance.

Block form: (for-each (x) body …) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: ANY — nil

(filter f)

A new set of the members passing a predicate.

Instance method: called on a Set instance.

Block form: (filter (x) body …) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: Set — A set of the members where (f x) is true

Examples:

(((Set of 1 2 3 4) filter (fn (_ x) (> x 2))) length) => 2

(map f)

A new set of the images of the members (duplicate images collapse).

Instance method: called on a Set instance.

Block form: (map (x) body …) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: Set — A set of the distinct (f x)

Examples:

(((Set of 1 2 3) map (fn (_ x) (* x 0))) length) => 1

(fold f acc)

Fold the members into an accumulator (unordered).

Instance method: called on a Set instance.

Block form: (fold (acc x) body … acc) – or (acc i x) with the 0-based index ahead of the element.

Parameters:

Returns: ANY — The final accumulator

Examples:

((Set of 1 2 3) fold (fn (_ a x) (+ a x)) 0) => 6

(->list)

The members as a list (unordered).

Instance method: called on a Set instance.

Returns: LIST — List of members