nest

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package nest implements the Nest generic container, used for generic inputs and outputs in GoMLX.

Index

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:

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

func Map[T any](stringMap map[string]T) *Nest[T]

Map creates a new Nest with the given valuesMap -- it is not copied, it's the same underlying map.

func Slice

func Slice[T any](values ...T) *Nest[T]

Slice creates a new Nest that is a slice initialized with the given slice -- the exact same slice, it is not copied.

func Unflatten

func Unflatten[T1, T2 any](nestShape *Nest[T1], flatValues []T2) *Nest[T2]

Unflatten will create a Nest[T2], using the given flatten values, and the structure and NestType of nestBase.

func Value

func Value[T any](value T) *Nest[T]

Value creates a new Nest that contains the given value.

func (*Nest[T]) Enumerate

func (n *Nest[T]) Enumerate(fn func(value T) error) error

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

func (n *Nest[T]) EnumerateWithPath(fn func(path string, value T) error) error

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

func (n *Nest[T]) IsEmpty() bool

IsEmpty returns whether the Nest is empty: only happens if it's invalid, or an empty slice or empty map.

func (*Nest[T]) IsMap

func (n *Nest[T]) IsMap() bool

IsMap returns whether the Nest is storing a map to T.

func (*Nest[T]) IsSlice

func (n *Nest[T]) IsSlice() bool

IsSlice returns whether the Nest is storing a slice.

func (*Nest[T]) IsValue

func (n *Nest[T]) IsValue() bool

IsValue returns whether the Nest holds a single value.

func (*Nest[T]) Map

func (n *Nest[T]) Map() map[string]T

Map returns a reference to the underlying map. It panics if the Nest is not of MapNest type.

func (*Nest[T]) Slice

func (n *Nest[T]) Slice() []T

Slice returns the slice contained in the Nest. It panics if the Nest is not of SliceNest type.

func (*Nest[T]) Value

func (n *Nest[T]) Value() T

Value returns the value stored in the Nest. It panics if the Nest is not a ValueNest.

type Type

type Type uint8

Type of Nest.

const (
	InvalidNest Type = iota
	ValueNest
	SliceNest
	MapNest
)

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL