nustack

iterable

Iterable capability.

Module nu.forms.collections.abc.iterable.

Iterable capability.

IterableForm: wrapping infrastructure for collection results.

Follows Python's collections.abc.Iterable pattern. In Nu's tree model, iteration is controlled by Flows (ForEachDo, ForRangeDo) and streams (.iter(), Map, Filter), never Python's iterator protocol: the Nu base blocks iter() / for on every term. This marks types as iterable and provides the wrapping infrastructure for typed results.

NameCallMeaning
IterableFormIterableForm()Base for values that support iteration, like collections.abc.Iterable.

IterableForm

Base for values that support iteration, like collections.abc.Iterable.

IterableForm()

Path nu.forms.collections.abc.IterableForm.

Subclasses (SequenceForm, MappingForm, etc.) use the wrapping hooks here to wrap op results in their own collection and element types. Higher-order interactions (Map, Filter, Reduce, etc.) are standalone functions in abc.fn, not methods on this class.

Example:

nu.List.of(1, 2, 3).iter()

Notes

  • Subclasses must override _wrap_iterable_result and _wrap_element_result.

Methods

.iter()

Open self into a lazy iterator stream over its elements.

Builds Iterator[ElementT].

Example: nu.ForEachDo(nu.List.of(1, 2, 3).iter(), body)

Yields

An Iterator streaming self's elements.

Notes

  • A pure read: builds the Iter stream query over self and wraps it as an Iterator.
  • Named because Python's iter() / for would loop over the term at build time; the Nu base blocks them.
  • The result is a lazy stream: loop it with nu.ForEachDo, drain it with .to_list(), or take its first item with .first().

Undocumented: example.

Inherited methods

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