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.
| Name | Call | Meaning |
|---|---|---|
| dict | core.dict(x) | Coerce x to a Nu Dict term. Dict(ToDict(x)) in one call. |
| float | core.float(x) | Coerce x to a Nu Float term. Float(ToFloat(x)) in one call. |
| format | core.format(value, spec=None) | Format value under spec into a Nu Str term. Str(Format(...)) in one call. |
| frozenset | core.frozenset(x) | Coerce x to a Nu FrozenSet term. FrozenSet(ToFrozenSet(x)) in one call. |
| int | core.int(x) | Coerce x to a Nu Int term. Int(ToInt(x)) in one call. |
| list | core.list(x) | Coerce x to a Nu List term. List(ToList(x)) in one call. |
| set | core.set(x) | Coerce x to a Nu Set term. Set(ToSet(x)) in one call. |
| sorted | core.sorted(x) | Sort x into a Nu List term. List(Sorted(x)) in one call. |
| str | core.str(x) | Coerce x to a Nu Str term. Str(ToStr(x)) in one call. |
| tuple | core.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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
value | object | the value to format. | |
spec | object | None | the 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 theStrsurface 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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
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
| Name | Type | Default | Meaning |
|---|---|---|---|
x | object |
Undocumented: example.