← index

Setsealed

inherits Object · mixes in Iterate

The insertion-ordered collection of unique values, written #<1 2 3>. Adding an element that is already present is a no-op.

Membership is hash-indexed: an element's hash runs once, at insert, and lookup uses the same equality ladder as Map keys — by value for scalars and strings, by identity for user instances unless their class overrides both hash and ==:. A set with a checked element type comes from Set.of: / ensure:.

extended at core/02-iterate.qn:615

Sets are iterable (each: is implemented natively - Set#each: in src/runtime/set.rs) and carry the set-algebra operations, built on the native add:/contains?: and the Iterate combinators.

extended at core/16-serialize.qn:60

A Set serializes as a List in insertion order (the formats have no set type); it comes back as a List — rebuild with Set.of: if set-ness matters.

Class methods

default

A fresh empty Set, the Set class default.

core/02-iterate.qn:620

new

A fresh empty set — the same value the #<> literal builds.

native

new:

Always refused: a Set has no instance fields for a new: config block to set. Construct with #< >, Set.new, or Set.of:.

native

of:

A fresh empty set tagged with an element class: every later add: is checked, raising a catchable TypeError on a mismatch.

(Set.of:String).elementType     "* -> String

native

Instance methods

==:

Same-elements equality: true when the other value is a Set of the same size containing every element of this one — insertion order does not matter. Anything that is not a Set answers false.

#<1 2 3> == #<3 2 1>     "* -> true

native

add:

Add a value; one already present is left alone (a set holds one of each). Answers the receiver. On a tagged set (Set.of:) the value is checked first.

#<1 2>.add:3     "* -> #<1 2 3>
#<1 2>.add:2     "* -> #<1 2>

native

asData

core/16-serialize.qn:61

collector

A fresh empty LIST carrying this set's element tag — the Iterate mixin's staging collection, where transforms like collect: accumulate results in order before they become the final collection.

native

contains?:

True when the value is an element — an O(1) hash probe, using the same equality ladder as Map keys.

#<1 2 3>.contains?:2     "* -> true
#<1 2 3>.contains?:9     "* -> false

native

count

The number of elements.

#<1 2 3>.count     "* -> 3

native

difference:

A new Set holding the elements of self that are not in other.

#<1 2 3>.difference:#<2>    "* -> #<1 3>

core/02-iterate.qn:651

each:

Call a block once per element, in insertion order; answers the receiver. This is the one iteration primitive — the whole Iterate surface (collect:, select:, …) derives from it.

native

elementType

The checked element type as a Symbol, or nil for an ordinary untagged set.

(Set.of:String).elementType     "* -> String

native

emptyLike

A fresh empty set like the receiver — element tag included. The species hook the Iterate mixin uses, so transforms of a checked set stay checked.

native

ensure:

Check every element against a class and answer a NEW set carrying that element tag; a non-matching element raises a catchable TypeError. Cached hashes carry over, so instance hash methods do not re-run.

(#<1 2>.ensure:Integer).elementType     "* -> Integer

native

intersection:

A new Set holding the elements present in both self and other.

#<1 2 3>.intersection:#<2 3 4>    "* -> #<2 3>

core/02-iterate.qn:640

remove:

Remove a value if present (a miss is a no-op); answers the receiver. The remaining elements keep their insertion order.

#<1 2 3>.remove:2     "* -> #<1 3>

native

s

The display string: #< and > around each element's .s, space-separated, in insertion order.

#<1 2 3>.s     "* -> #<1 2 3>

native

subset?:

True when every element of self is in other.

#<1 2>.subset?:#<1 2 3>    "* -> true

core/02-iterate.qn:662

superset?:

True when every element of other is in self.

#<1 2 3>.superset?:#<2>    "* -> true

core/02-iterate.qn:669

union:

A new Set holding every element of self and other.

#<1 2>.union:#<2 3>    "* -> #<1 2 3>

core/02-iterate.qn:628