nustack

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 MutableSetForm = SetLike + add/remove/discard/pop/clear/update/*_update

Set algebra is named methods only. &, | and >> compose flows on every term (a set form included), and - / ^ are not defined here.

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 on top of SetLikeForm. The in-place spellings are the *_update methods.

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, missing_ok=False)

Remove value from self.

Builds Any.

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to remove.
missing_okboolFalsewhen True, an absent value is a no-op. A plain Python bool fixed at construction, not a Nu value.

Yields

Nothing (Command). Mutates self in place.

Notes

  • By default raises at evaluation time when value is absent, matching Python's set.remove.

Example

class Port(nu.Shape):
    members = nu.SetRef.slot(str)
data = {"members": {"a", "b"}}
ctx = nu.Context().bind(dict, data, Port)
_ = nu.run(Port.members.remove("a"), ctx)
_ = nu.run(Port.members.remove("z", missing_ok=True), ctx)
data
{'members': {'b'}}

.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.

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()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

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

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

From nu.lang.forms.Form:

CallBuildsMeaning
a == bBoolSelf equal to other by value: an Eq term, never a Python bool.
a != bBoolSelf not equal to other by value: a Ne term, never a Python bool.
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

From builtins.object:

CallBuildsMeaning
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.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, missing_ok=False)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.

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()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

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

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

From nu.lang.forms.Form:

CallBuildsMeaning
a == bBoolSelf equal to other by value: an Eq term, never a Python bool.
a != bBoolSelf not equal to other by value: a Ne term, never a Python bool.
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

From builtins.object:

CallBuildsMeaning
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. Named methods only: &, | and >> compose flows on a set like on every term, and - / ^ are not defined.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self or other is EMPTY.

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. EMPTY when self is EMPTY.

Example

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

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()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

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

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

From nu.lang.forms.Form:

CallBuildsMeaning
a == bBoolSelf equal to other by value: an Eq term, never a Python bool.
a != bBoolSelf not equal to other by value: a Ne term, never a Python bool.
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

From builtins.object:

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

On this page