nustack

cast_fns

Cast wrappers: coerce `x` into a Nu term of the target Form.

Module nu.core.cast_fns.

Cast wrappers: coerce x into a Nu term of the target Form.

Lives in a separate module from cast because these functions shadow the Python builtins (str, int, list, ...) and would collide with the same names used as type annotations inside cast.py.

sorted and format sit here too: not casts, but the same kind of builtin mirror, wrapping their query in the Form it yields.

NameCallMeaning
dictcore.dict(x)Coerce x to a Nu Dict term. Dict(ToDict(x)) in one call.
floatcore.float(x)Coerce x to a Nu Float term. Float(ToFloat(x)) in one call.
formatcore.format(value, spec=None)Format value under spec into a Nu Str term. Str(Format(...)) in one call.
frozensetcore.frozenset(x)Coerce x to a Nu FrozenSet term. FrozenSet(ToFrozenSet(x)) in one call.
intcore.int(x)Coerce x to a Nu Int term. Int(ToInt(x)) in one call.
listcore.list(x)Coerce x to a Nu List term. List(ToList(x)) in one call.
setcore.set(x)Coerce x to a Nu Set term. Set(ToSet(x)) in one call.
sortedcore.sorted(x)Sort x into a Nu List term. List(Sorted(x)) in one call.
strcore.str(x)Coerce x to a Nu Str term. Str(ToStr(x)) in one call.
tuplecore.tuple(x)Coerce x to a Nu Tuple term. Tuple(ToTuple(x)) in one call.

dict

Coerce x to a Nu Dict term. Dict(ToDict(x)) in one call.

core.dict(x)

Path nu.core.dict. Defined on nu.core.cast_fns, bound as a function. Builds Dict.

For building a dict from named Nu expressions, use nu.Dict.of(...).

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

float

Coerce x to a Nu Float term. Float(ToFloat(x)) in one call.

core.float(x)

Path nu.core.float. Defined on nu.core.cast_fns, bound as a function. Builds Float.

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

format

Format value under spec into a Nu Str term. Str(Format(...)) in one call.

core.format(value, spec=None)

Path nu.core.format. Defined on nu.core.cast_fns, bound as a function. Builds Str.

Arguments

NameTypeDefaultMeaning
valueobjectthe value to format.
specobjectNonethe format spec, e.g. ".2f". Left out, the empty spec applies, as with Python's format(value).

Notes

  • Always a string, so it reads as one: .upper(), + and the rest of the Str surface take it as is.

Example

nu.run(nu.format(3.14159, ".2f") + "s")[0]
nu.run(nu.format(7))[0]
'3.14s'
'7'

frozenset

Coerce x to a Nu FrozenSet term. FrozenSet(ToFrozenSet(x)) in one call.

core.frozenset(x)

Path nu.core.frozenset. Defined on nu.core.cast_fns, bound as a function. Builds FrozenSet.

For building a frozenset from positional Nu expressions, use nu.FrozenSet.of(...).

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

int

Coerce x to a Nu Int term. Int(ToInt(x)) in one call.

core.int(x)

Path nu.core.int. Defined on nu.core.cast_fns, bound as a function. Builds Int.

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

list

Coerce x to a Nu List term. List(ToList(x)) in one call.

core.list(x)

Path nu.core.list. Defined on nu.core.cast_fns, bound as a function. Builds List.

For building a list from positional Nu expressions, use nu.List.of(...).

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

set

Coerce x to a Nu Set term. Set(ToSet(x)) in one call.

core.set(x)

Path nu.core.set. Defined on nu.core.cast_fns, bound as a function. Builds Set.

For building a set from positional Nu expressions, use nu.Set.of(...).

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

sorted

Sort x into a Nu List term. List(Sorted(x)) in one call.

core.sorted(x)

Path nu.core.sorted. Defined on nu.core.cast_fns, bound as a function. Builds List.

Arguments

NameTypeDefaultMeaning
xobject

Example

nu.run(nu.sorted([3, 1, 2]).slice(0, 2))[0]
[1, 2]

str

Coerce x to a Nu Str term. Str(ToStr(x)) in one call.

core.str(x)

Path nu.core.str. Defined on nu.core.cast_fns, bound as a function. Builds Str.

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

tuple

Coerce x to a Nu Tuple term. Tuple(ToTuple(x)) in one call.

core.tuple(x)

Path nu.core.tuple. Defined on nu.core.cast_fns, bound as a function. Builds Tuple.

For building a tuple from positional Nu expressions, use nu.Tuple.of(...).

Arguments

NameTypeDefaultMeaning
xobject

Undocumented: example.

On this page