nustack

sequence

Sequence collection: bases + mutations.

Module nu.forms.collections.abc.sequence.

Sequence collection: bases + mutations.

SequenceForm = Collection + Sliceable + first/last/index/count/reversed MutableSequenceForm = Sequence + append/insert/pop/extend/remove/reverse

Sorted/Reversed are standalone core atoms (nu.Sorted, nu.Reversed).

Follows Python's collections.abc.Sequence / MutableSequence pattern.

NameCallMeaning
MutableSequenceFormMutableSequenceForm()Base for in-place-mutable sequences, like collections.abc.MutableSequence.
ReactiveSequenceFormReactiveSequenceForm()Sequence with change subscriptions layered on MutableSequenceForm.
SequenceFormSequenceForm()Base for ordered, indexable values, like collections.abc.Sequence.

MutableSequenceForm

Base for in-place-mutable sequences, like collections.abc.MutableSequence.

MutableSequenceForm()

Path nu.forms.collections.abc.MutableSequenceForm.

Notes

  • Every mutator here needs a Ref on the left; none of them evaluate standalone against a plain sequence value.
  • pop is the one Action (mutates and returns a value); the rest are Commands (mutate, yield nothing).

Methods

.append(value)

Append value to the end of self.

Builds Any.

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to append.

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.extend(other)

Extend self with the elements of other, in order.

Builds Any.

Arguments

NameTypeDefaultMeaning
otherArg[Iterable[ElementT]]the iterable of elements to append.

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.insert(index, value)

Insert value at index, shifting later elements right.

Builds Any.

Arguments

NameTypeDefaultMeaning
indexIntArgthe position to insert at. Out-of-range indices clamp like Python's list.insert rather than raising.
valueArg[ElementT]the element to insert.

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.pop(index=-1)

Remove and return the element at index.

Builds ElementResultT.

Arguments

NameTypeDefaultMeaning
indexIntArg-1the position to remove. Defaults to the last element.

Yields

The removed element (Action). EMPTY when index is out of range. An EMPTY self or index raises, as any write does.

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.del_at(index)

Remove the element at index.

Builds Any.

Arguments

NameTypeDefaultMeaning
indexIntArgthe position to remove. An out-of-range index raises at evaluation time, matching Python's del seq[i].

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.
  • Use this over pop when a Command is wanted instead of the Action pop yields.

Undocumented: example.

.remove(value, missing_ok=False)

Remove the first occurrence of value.

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

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.
  • By default a value that isn't present raises at evaluation time, matching Python's list.remove.

Example

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

.reverse()

Reverse self in place.

Builds Any.

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.sort()

Sort self in place, ascending, using the elements' natural order.

Builds Any.

Yields

Nothing (Command).

Notes

  • No key support yet; sorting by a derived key is deferred.
  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

.copy()

Shallow copy of self: a new sequence with the same elements.

Builds CollectionResultT.

Yields

The copy. EMPTY when self is EMPTY.

Notes

  • Unlike the mutators above, this is a Query: it doesn't mutate self and evaluates fine against a plain sequence value.

Example

nu.run(nu.List.of(1, 2, 3).copy())[0]
[1, 2, 3]

.clear()

Remove every element from self.

Builds Any.

Yields

Nothing (Command).

Notes

  • Mutates self and needs a Ref on the left; it can't run standalone against a plain sequence value.

Undocumented: example.

Inherited methods

From nu.forms.collections.abc.sequence.SequenceForm:

CallBuildsMeaning
a[key]ElementResultT | CollectionResultTElement at an int index, or subsequence for a slice.
.first_elem()ElementResultTFirst element of self.
.last_elem()ElementResultTLast element of self.
.index(value)IntLowest index in self where value is found, searching from the left.
.count(value)IntCount of occurrences of value in self.
.reversed()CollectionResultTSelf walked back to front, as a stream.

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.forms.collections.abc.sliceable.SliceableForm:

CallBuildsMeaning
.slice(start, stop, step=None)ResultTSlice of self from start to stop, stepping by step.

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.

ReactiveSequenceForm

Sequence with change subscriptions layered on MutableSequenceForm.

ReactiveSequenceForm()

Path nu.forms.collections.abc.ReactiveSequenceForm.

Notes

  • Adds on_change for any-change observation on this slot. The three tree-aware variants (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 sequence slot.

Builds object.

Yields

An event stream firing whenever this slot changes.

Undocumented: example.

Inherited methods

From nu.forms.collections.abc.sequence.MutableSequenceForm:

CallBuildsMeaning
.append(value)AnyAppend value to the end of self.
.extend(other)AnyExtend self with the elements of other, in order.
.insert(index, value)AnyInsert value at index, shifting later elements right.
.pop(index=-1)ElementResultTRemove and return the element at index.
.del_at(index)AnyRemove the element at index.
.remove(value, missing_ok=False)AnyRemove the first occurrence of value.
.reverse()AnyReverse self in place.
.sort()AnySort self in place, ascending, using the elements' natural order.
.copy()CollectionResultTShallow copy of self: a new sequence with the same elements.
.clear()AnyRemove every element from self.

From nu.forms.collections.abc.sequence.SequenceForm:

CallBuildsMeaning
a[key]ElementResultT | CollectionResultTElement at an int index, or subsequence for a slice.
.first_elem()ElementResultTFirst element of self.
.last_elem()ElementResultTLast element of self.
.index(value)IntLowest index in self where value is found, searching from the left.
.count(value)IntCount of occurrences of value in self.
.reversed()CollectionResultTSelf walked back to front, as a stream.

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.forms.collections.abc.sliceable.SliceableForm:

CallBuildsMeaning
.slice(start, stop, step=None)ResultTSlice of self from start to stop, stepping by step.

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.

SequenceForm

Base for ordered, indexable values, like collections.abc.Sequence.

SequenceForm()

Path nu.forms.collections.abc.SequenceForm.

Notes

  • Mixed in by List, Str, Bytes, Tuple, ListRef; this class only carries the shared shape. Concrete carriers narrow CollectionResultT/ElementResultT to their own types.
  • Order is positional and stable: iteration, indexing, and first_elem/last_elem all agree on the same order.

Methods

a[key]

Element at an int index, or subsequence for a slice.

Builds ElementResultT | CollectionResultT.

Arguments

NameTypeDefaultMeaning
keyIntArg | slicean int index, or a Python slice of int start/stop/step.

Yields

The element for an int key, the subsequence for a slice. EMPTY when self is EMPTY.

Notes

  • An out-of-range int index raises at evaluation time, matching Python. A slice never raises; out-of-range bounds clamp like Python slicing, so a slice past the end yields a shorter (or empty) result rather than erroring.
  • Negative indices and negative slice bounds work as in Python.

Examples

nu.run(nu.List.of(1, 2, 3)[1])[0]
2
nu.run(nu.List.of(1, 2, 3)[1:3])[0]
[2, 3]

.first_elem()

First element of self.

Builds ElementResultT.

Yields

The first element. EMPTY when self has no elements or is EMPTY.

Examples

nu.run(nu.List.of(1, 2, 3).first_elem())[0]
1
nu.run(nu.List.of().first_elem())[0]
<EMPTY>

.last_elem()

Last element of self.

Builds ElementResultT.

Yields

The last element. EMPTY when self has no elements or is EMPTY.

Example

nu.run(nu.List.of(1, 2, 3).last_elem())[0]
3

.index(value)

Lowest index in self where value is found, searching from the left.

Builds Int.

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to search for.

Yields

The lowest matching index. EMPTY when self is EMPTY or value is not found.

Notes

  • Unlike Python's list.index, a missing value does not raise: it yields EMPTY instead.

Examples

nu.run(nu.List.of(1, 2, 3).index(2))[0]
1
nu.run(nu.List.of(1, 2, 3).index(5))[0]
<EMPTY>

.count(value)

Count of occurrences of value in self.

Builds Int.

Arguments

NameTypeDefaultMeaning
valueArg[ElementT]the element to count.

Yields

The occurrence count, 0 when value is absent. EMPTY when self is EMPTY.

Example

nu.run(nu.List.of(1, 2, 2, 3).count(2))[0]
2

.reversed()

Self walked back to front, as a stream.

Builds CollectionResultT.

Yields

The elements of self in reverse order.

Notes

  • Yields a stream, not a materialized copy; consume it through a Flow (ForEachDo) or a Ref-backed context. It doesn't evaluate standalone as a scalar against a plain sequence value.

Undocumented: example.

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.forms.collections.abc.sliceable.SliceableForm:

CallBuildsMeaning
.slice(start, stop, step=None)ResultTSlice of self from start to stop, stepping by step.

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.

On this page