Documentation
¶
Overview ¶
Package nest implements the Nest generic container, used for generic inputs and outputs in GoMLX.
Index ¶
- type Nest
- func (n *Nest[T]) Enumerate(fn func(value T) error) error
- func (n *Nest[T]) EnumerateWithPath(fn func(path string, value T) error) error
- func (n *Nest[T]) Flatten() []T
- func (n *Nest[T]) IsEmpty() bool
- func (n *Nest[T]) IsMap() bool
- func (n *Nest[T]) IsSlice() bool
- func (n *Nest[T]) IsValue() bool
- func (n *Nest[T]) Map() map[string]T
- func (n *Nest[T]) Slice() []T
- func (n *Nest[T]) Value() T
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Nest ¶
type Nest[T any] struct { // contains filtered or unexported fields }
Nest is a generic container used in GoMLX in several of its libraries. It is a "sum type" (a union) of the value T itself, a map of string to T, a tuple of T values or an invalid Nest[T], which may contain an error message. It is only one of those and accessing the wrong instance will panic -- except for Error, which may be checked at any time.
Future Work:
- Make new types for recursive Nest: if one wants a slice of maps, or a map of slices, etc. Not sure if needed, but it could be expanded to that.
func Map ¶
Map creates a new Nest with the given valuesMap -- it is not copied, it's the same underlying map.
func Slice ¶
Slice creates a new Nest that is a slice initialized with the given slice -- the exact same slice, it is not copied.
func Unflatten ¶
Unflatten will create a Nest[T2], using the given flatten values, and the structure and NestType of nestBase.
func (*Nest[T]) Enumerate ¶
Enumerate all values stored in a Nest, in a deterministic order and call `fn` for each value. If fn returns an error, Enumerate will exit immediately and return the corresponding error.
func (*Nest[T]) EnumerateWithPath ¶
EnumerateWithPath all values stored in a Nest, in a deterministic order and call `fn` with the path to the element and the corresponding value. If fn returns an error, Enumerate will exit immediately and return the corresponding error.
func (*Nest[T]) Flatten ¶
func (n *Nest[T]) Flatten() []T
Flatten converts the Nest of any time to a slice, in a deterministic fashion. This is trivial if the Nest is already a slice, but this is a common interface for all Nest types.
Notice the returned slice should not be changed, the underlying space is owned by the Nest.
If the Nest is of an invalid type, it returns nil.
func (*Nest[T]) IsEmpty ¶
IsEmpty returns whether the Nest is empty: only happens if it's invalid, or an empty slice or empty map.
func (*Nest[T]) Map ¶
Map returns a reference to the underlying map. It panics if the Nest is not of MapNest type.