nustack
ReferenceNuformscollectionsabc

set_

Set collection: bases + mutations.

Module nu.forms.collections.abc.set_.

Set collection: bases + mutations.

SetLikeForm = Collection + union/intersection/difference/symmetric_difference/issubset/issuperset/isdisjoint + copy + or/and/sub/xor MutableSetForm = SetLike + add/remove/discard/pop/clear/update/*_update + ior/iand/isub/ixor

Follows Python's collections.abc.Set / MutableSet pattern.

NameCallMeaning
MutableSetFormMutableSetForm()Base for mutable set values, like collections.abc.MutableSet.
ReactiveSetFormReactiveSetForm()Reactive set. Adds on_change() for any-change observation.
SetLikeFormSetLikeForm()Base for set values, like collections.abc.Set.

MutableSetForm

Base for mutable set values, like collections.abc.MutableSet.

MutableSetForm()

Path nu.forms.collections.abc.MutableSetForm.

Adds add/remove/discard/pop/clear/update/*_update and the in-place operators |= &= -= ^= on top of SetLikeForm.

Notes

  • Every mutating method needs self bound to a Ref inside a shape; none of them run on a bare set literal.

Methods

.add(value)

Add value to self.

Builds Any.

Example: my_set.add(4)

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to add.

Yields

Nothing (Command). Mutates self in place.

Notes

  • A no-op if value is already present, matching Python's set.add.

Undocumented: example.

.remove(value)

Remove value from self.

Builds Any.

Example: my_set.remove(4)

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to remove.

Yields

Nothing (Command). Mutates self in place. Raises at evaluation time when value is not in self.

Notes

  • Raises at evaluation time when value is absent. Use discard when a missing value shouldn't raise.

Undocumented: example.

.discard(value)

Remove value from self if present.

Builds Any.

Example: my_set.discard(4)

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to remove.

Yields

Nothing (Command). Mutates self in place.

Notes

  • Unlike remove, a missing value is not an error.

Undocumented: example.

.pop()

Remove and return an arbitrary element from self.

Builds ElementResultT.

Example: my_set.pop()

Yields

The removed element. Mutates self in place. Raises at evaluation time when self is empty.

Notes

  • Which element comes out is unspecified; do not rely on an order.
  • Raises at evaluation time when self is empty.

Undocumented: example.

.clear()

Remove every element from self.

Builds Any.

Example: my_set.clear()

Yields

Nothing (Command). Mutates self in place, leaving it empty.

Undocumented: example.

.update(other)

Add every element of other to self.

Builds Any.

Example: my_set.update({4, 5})

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set whose elements are added to self.

Yields

Nothing (Command). Mutates self in place.

Undocumented: example.

.intersection_update(other)

Keep only the elements of self also found in other.

Builds Any.

Example: my_set.intersection_update({2, 3})

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to intersect self with.

Yields

Nothing (Command). Mutates self in place.

Undocumented: example.

.difference_update(other)

Remove every element of other from self.

Builds Any.

Example: my_set.difference_update({2, 3})

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set of elements to remove from self.

Yields

Nothing (Command). Mutates self in place.

Undocumented: example.

.symmetric_difference_update(other)

Keep the elements in exactly one of self and other.

Builds Any.

Example: my_set.symmetric_difference_update({2, 4})

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to compare self against.

Yields

Nothing (Command). Mutates self in place.

Undocumented: example.

Inherited methods

From nu.forms.collections.abc.set_.SetLikeForm:

CallBuildsMeaning
.union(other)CollectionResultTUnion of self and other.
.intersection(other)CollectionResultTIntersection of self and other.
.difference(other)CollectionResultTElements of self that are not in other.
.symmetric_difference(other)CollectionResultTElements in exactly one of self and other, not both.
.issubset(other)BoolWhether every element of self is in other.
.issuperset(other)BoolWhether every element of other is in self.
.isdisjoint(other)BoolWhether self and other share no elements.
.copy()CollectionResultTShallow copy of self.
a | bCollectionResultTUnion: self | other.
a & bCollectionResultTIntersection: self & other.
a - bCollectionResultTDifference: self - other.
a ^ bCollectionResultTSymmetric difference: self ^ other.

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
iter(a)Iterator[ElementT]Open self into a lazy iterator stream (Python's iter).

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

From builtins.object:

CallBuildsMeaning
a < bReturn self<value.
a <= bReturn self<=value.
a == bReturn self==value.
a != bReturn self!=value.
a > bReturn self>value.
a >= bReturn self>=value.

Undocumented: example.

ReactiveSetForm

Reactive set. Adds on_change() for any-change observation.

ReactiveSetForm()

Path nu.forms.collections.abc.ReactiveSetForm.

Example: my_set.on_change()

Notes

  • The three tree-aware methods (on_child_change, on_children_change, on_descendants_change) are shape-domain and live on nu.domains.shape.forms.collection.ReactiveCollectionForm, not here.

Methods

.on_change()

Subscribe to any change on this set slot.

Builds object.

Example: my_set.on_change()

Yields

An OnChange observable that fires whenever self changes.

Undocumented: example.

Inherited methods

From nu.forms.collections.abc.set_.MutableSetForm:

CallBuildsMeaning
.add(value)AnyAdd value to self.
.remove(value)AnyRemove value from self.
.discard(value)AnyRemove value from self if present.
.pop()ElementResultTRemove and return an arbitrary element from self.
.clear()AnyRemove every element from self.
.update(other)AnyAdd every element of other to self.
.intersection_update(other)AnyKeep only the elements of self also found in other.
.difference_update(other)AnyRemove every element of other from self.
.symmetric_difference_update(other)AnyKeep the elements in exactly one of self and other.

From nu.forms.collections.abc.set_.SetLikeForm:

CallBuildsMeaning
.union(other)CollectionResultTUnion of self and other.
.intersection(other)CollectionResultTIntersection of self and other.
.difference(other)CollectionResultTElements of self that are not in other.
.symmetric_difference(other)CollectionResultTElements in exactly one of self and other, not both.
.issubset(other)BoolWhether every element of self is in other.
.issuperset(other)BoolWhether every element of other is in self.
.isdisjoint(other)BoolWhether self and other share no elements.
.copy()CollectionResultTShallow copy of self.
a | bCollectionResultTUnion: self | other.
a & bCollectionResultTIntersection: self & other.
a - bCollectionResultTDifference: self - other.
a ^ bCollectionResultTSymmetric difference: self ^ other.

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
iter(a)Iterator[ElementT]Open self into a lazy iterator stream (Python's iter).

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

From builtins.object:

CallBuildsMeaning
a < bReturn self<value.
a <= bReturn self<=value.
a == bReturn self==value.
a != bReturn self!=value.
a > bReturn self>value.
a >= bReturn self>=value.

Undocumented: example.

SetLikeForm

Base for set values, like collections.abc.Set.

SetLikeForm()

Path nu.forms.collections.abc.SetLikeForm.

Ops: union, intersection, difference, symmetric_difference, issubset, issuperset, isdisjoint, copy, and operators | & - ^.

Notes

  • Subclasses (e.g. Set) must implement _wrap_set_result to wrap a query result in their own concrete set type.
  • Every op accepts a plain Python set/frozenset or another set-like form as the right operand.

Example

nu.run(nu.Set({1, 2}).union({2, 3}))[0]
{1, 2, 3}

Methods

.union(other)

Union of self and other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to union with self.

Yields

A new set with every element from self and other. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2}).union({2, 3}))[0]
{1, 2, 3}

.intersection(other)

Intersection of self and other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to intersect with self.

Yields

A new set with only the elements found in both. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).intersection({2, 3, 4}))[0]
{2, 3}

.difference(other)

Elements of self that are not in other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to subtract from self.

Yields

A new set with the elements of self minus the elements of other. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).difference({2, 3}))[0]
{1}

.symmetric_difference(other)

Elements in exactly one of self and other, not both.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to compare against self.

Yields

A new set with the elements that are in self or other but not in both. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).symmetric_difference({2, 3, 4}))[0]
{1, 4}

.issubset(other)

Whether every element of self is in other.

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to test self against.

Yields

True when self is a subset of other (equal sets count), False otherwise. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).issubset({1, 2, 3, 4}))[0]
True

.issuperset(other)

Whether every element of other is in self.

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to test against self.

Yields

True when self is a superset of other (equal sets count), False otherwise. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).issuperset({1, 2}))[0]
True

.isdisjoint(other)

Whether self and other share no elements.

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to test against self.

Yields

True when self and other have no elements in common, False otherwise. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).isdisjoint({9, 10}))[0]
True

.copy()

Shallow copy of self.

Builds CollectionResultT.

Yields

A new set with the same elements as self. INVALID when self is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}).copy())[0]
{1, 2, 3}

a | b

Union: self | other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to union with self.

Yields

A new set with every element from self and other, same as union. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}) | {4, 5})[0]
{1, 2, 3, 4, 5}

a & b

Intersection: self & other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to intersect with self.

Yields

A new set with only the elements found in both, same as intersection. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}) & {2, 3})[0]
{2, 3}

a - b

Difference: self - other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to subtract from self.

Yields

A new set with the elements of self minus other, same as difference. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}) - {2})[0]
{1, 3}

a ^ b

Symmetric difference: self ^ other.

Builds CollectionResultT.

Arguments

NameTypeDefaultMeaning
otherArg[set[ElementT] | frozenset[ElementT]]the set to compare against self.

Yields

A new set with the elements in self or other but not both, same as symmetric_difference. INVALID when self or other is a sentinel.

Example

nu.run(nu.Set({1, 2, 3}) ^ {2, 4})[0]
{1, 3, 4}

Inherited methods

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
iter(a)Iterator[ElementT]Open self into a lazy iterator stream (Python's iter).

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

From builtins.object:

CallBuildsMeaning
a < bReturn self<value.
a <= bReturn self<=value.
a == bReturn self==value.
a != bReturn self!=value.
a > bReturn self>value.
a >= bReturn self>=value.

On this page