nustack

attrs.refs

`Attr`: the tree's one read of `ctx.attrs`.

Module nu.context.attrs.refs.

Attr: the tree's one read of ctx.attrs.

An Attr names a key in ctx.attrs by its resolved address (any child that yields a value) and self-yields what an interaction bound there. It only reads: interactions write attrs in their own compile, and the tree never does.

NameSortCallMeaning
AttrrefAttr(address)Reads the value an interaction handed its body under a name.

Attr

Reads the value an interaction handed its body under a name.

Attr(address)

Path nu.context.Attr. Kind Ref, sort ref, cardinality scalar.

A loop binds its item, a fold its accumulator, a catch the error, a retry the attempt, a reaction the key that changed. Attr(name) is how the body reads it. A body written as a lambda never spells one out: the interaction mints the name and hands the lambda an Attr over it. The sole child is the address, evaluated through the runtime like any other child, so a name can be fixed at write time or computed at run time.

Notes

  • Read-only. Nothing in the tree binds or reassigns a name; state goes through a fabric (nu.mem for local state).
  • It carries the Object form, the surface every term has. Wrap the read in the form it needs: nu.Str(nu.Attr("item")).upper().
  • A name nothing bound and a name bound to EMPTY read the same, so reach for .exists() when the difference matters.

Examples

nu.run(nu.Attr("missing"))[0]
<EMPTY>
nu.run(nu.Collect(nu.Map(nu.Iter([1, 2]), nu.Int(nu.Attr("item")) + 1)))[0]
[2, 3]

Methods

.exists()

Whether this Ref's name is bound in ctx.attrs, as a Bool.

Builds Bool.

Notes

  • The plain read cannot answer this: an unbound name yields EMPTY and so does a name bound to EMPTY.
  • Only the address is resolved; the value is never read.
  • Always a bool, so it reads as one: .not_(), .and_().

Example

nu.run(nu.context.Attr("total").exists().not_())[0]
True

Inherited methods

From nu.forms.primitives.object_.Object:

CallBuildsMeaning
a + bObjectSum of self and other.
a - bObjectSelf minus other.
a * bObjectProduct of self and other.
a @ bObjectMatrix multiplication: self @ other.
a / bObjectSelf divided by other.
a // bObjectSelf floor-divided by other.
a % bObjectSelf modulo other.
a ** bObjectSelf raised to the other power.
-aObjectNegation of self.
+aObjectSelf unchanged.
abs(a)ObjectAbsolute value of self.
a > bBoolSelf strictly greater than other.
a < bBoolSelf strictly less than other.
a >= bBoolSelf greater than or equal to other.
a <= bBoolSelf less than or equal to other.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.
.bitand(other)ObjectPython's & on the values: bits for ints, intersection for sets.
.bitor(other)ObjectPython's | on the values: bits for ints, union for sets, merge for dicts.
.bitxor(other)ObjectPython's ^ on the values: bits for ints, symmetric difference for sets.
.bitnot()ObjectPython's ~ on the value.
.lshift(other)ObjectSelf shifted left by other bits: Python's << on the values.
.rshift(other)ObjectSelf shifted right by other bits: Python's >> on the values.
.union(other)ObjectSet union of self and other.
.intersection(other)ObjectSet intersection of self and other.
.difference(other)ObjectElements of self that are not in other.
.symmetric_difference(other)ObjectElements in exactly one of self and other.
.merge(other)ObjectSelf and other merged into a new mapping: Python's dict |.
a[key]ObjectSubscript access: self[key].
.len()IntLength of self.
.contains(item)BoolMembership test: item in self.
.iter()IteratorLazy iterator stream over self's elements.
.has_attr(name)BoolAttribute presence: hasattr(self, name).

From nu.lang.forms.Form:

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

On this page