dt

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 23 Imported by: 19

Documentation

Overview

Package dt provides container type implementations and interfaces.

All top level structures in this package can be trivially constructed and provide high level interfaces for most common operations. These structures are not safe for access from multiple concurrent go routines (see the queue/deque in the pubsub package as an alternative for these use cases.)

Index

Constants

View Source
const ErrUninitializedContainer ers.Error = ers.Error("uninitialized container")

ErrUninitializedContainer is the content of the panic produced when you attempt to perform an operation on an uninitialized sequence.

Variables

This section is empty.

Functions

func ConsumePairs added in v0.10.3

func ConsumePairs[K comparable, V any](iter *fun.Stream[Pair[K, V]]) fun.Generator[*Pairs[K, V]]

ConsumePairs creates a *Pairs[K,V] object from a stream of Pair[K,V] objects.

func ConsumeTuples added in v0.10.5

func ConsumeTuples[K any, V any](iter *fun.Stream[Tuple[K, V]]) fun.Generator[*Tuples[K, V]]

ConsumeTuples creates a *Tuples[K,V] object from a stream of Tuple[K,V] objects.

func DefaultMap added in v0.10.4

func DefaultMap[K comparable, V any](input map[K]V, args ...int) map[K]V

DefaultMap takes a map value and returns it if it's non-nil. If the map is nil, it constructs and returns a new map, with the (optionally specified length.

func Transform

func Transform[T any, O any](in Slice[T], op fun.Converter[T, O]) fun.Generator[Slice[O]]

Transform processes a slice of one type into a slice of another type using the transformation function. Errors abort the transformation, with the exception of fun.ErrStreamContinue. All errors are returned to the caller, except io.EOF which indicates the (early) end of iteration.

func Unwind

func Unwind[T any](in T) []T

Unwind uses the Unwrap operation to build a list of the "wrapped" objects.

func Unwrap

func Unwrap[T any](in T) (out T)

Unwrap is a generic equivalent of the `errors.Unwrap()` function for any type that implements an `Unwrap() T` method. useful in combination with Is.

Types

type Element

type Element[T any] struct {
	// contains filtered or unexported fields
}

Element is the underlying component of a list, provided by streams, the Pop operations, and the Front/Back accesses in the list. You can use the methods on this objects to iterate through the list, and the Ok() method for validating zero-valued items.

func NewElement

func NewElement[T any](val T) *Element[T]

NewElement produces an unattached Element that you can use with Append. Element.Append(NewElement()) is essentially the same as List.PushBack().

func (*Element[T]) Append

func (e *Element[T]) Append(val *Element[T]) *Element[T]

Append adds the element 'new' after the element 'e', inserting it in the next position in the list. Will return 'e' if 'new' is not valid for insertion into this list (e.g. it belongs to another list, or is attached to other elements, is already a member of this list, or is otherwise invalid.) PushBack and PushFront, are implemented in terms of Append.

func (*Element[T]) Drop

func (e *Element[T]) Drop()

Drop wraps remove, and additionally, if the remove was successful, drops the value and sets the Ok value to false.

func (*Element[T]) In

func (e *Element[T]) In(l *List[T]) bool

In checks to see if an element is in the specified list. Because elements hold a pointer to their list, this is an O(1) operation.

Returns false when the element is nil.

func (*Element[T]) MarshalJSON

func (e *Element[T]) MarshalJSON() ([]byte, error)

MarshalJSON satisfies the json.Marshaler interface. Nil and unset Element values are marshaled as nil.

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next produces the next element. This is always non-nil, *unless* the element is not a member of a list. At the ends of a list, the value is non-nil, but would return false for Ok.

func (*Element[T]) Ok

func (e *Element[T]) Ok() bool

Ok checks that an element is valid. Invalid elements can be produced at the end of iterations (e.g. the list's root object,) or if you attempt to Pop an element off of an empty list.

Returns false when the element is nil.

func (*Element[T]) Previous

func (e *Element[T]) Previous() *Element[T]

Previous produces the next element. This is always non-nil, *unless* the element is not a member of a list. At the ends of a list, the value is non-nil, but would return false for Ok.

func (*Element[T]) Push added in v0.12.0

func (e *Element[T]) Push(v T) *Element[T]

Push adds a value to the list, and returns the resulting element.

func (*Element[T]) Remove

func (e *Element[T]) Remove() bool

Remove removes the elemtn from the list, returning true if the operation was successful. Remove returns false when the element is not valid to be removed (e.g. is not part of a list, is the root element of the list, etc.)

func (*Element[T]) Set

func (e *Element[T]) Set(v T) bool

Set allows you to change set the value of an item in place. Returns true if the operation is successful. The operation fails if the Element is the root item in a list or not a member of a list.

Set is safe to call on nil elements.

func (*Element[T]) String

func (e *Element[T]) String() string

String returns the string form of the value of the element.

func (*Element[T]) Swap

func (e *Element[T]) Swap(with *Element[T]) bool

Swap exchanges the location of two elements in a list, returning true if the operation was successful, and false if the elements are not eligible to be swapped. It is valid/possible to swap the root element of the list with another element to "move the head", causing a wrap around effect. Swap will not operate if either element is nil, or not a member of the same list.

func (*Element[T]) UnmarshalJSON

func (e *Element[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads the json value, and sets the value of the element to the value in the json, potentially overriding an existing value. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Element[T]) Value

func (e *Element[T]) Value() T

Value accesses the element's value.

type Heap

type Heap[T any] struct {
	LT cmp.LessThan[T]
	// contains filtered or unexported fields
}

Heap provides a min-order heap using the Heap.LT comparison operator to sort from lowest to highest. Push operations will panic if LT is not set.

func (*Heap[T]) Handler added in v0.12.0

func (h *Heap[T]) Handler() fn.Handler[T]

func (*Heap[T]) Len

func (h *Heap[T]) Len() int

Len reports the size of the heap. Because the heap tracks its size with Push/Pop operations, this is a constant time operation.

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (T, bool)

Pop removes the element from the underlying list and returns it, with an Ok value, which is true when the value returned is valid.

func (*Heap[T]) Populate added in v0.12.0

func (h *Heap[T]) Populate(iter *fun.Stream[T]) fun.Worker

func (*Heap[T]) Push

func (h *Heap[T]) Push(t T)

Push adds an item to the heap.

func (*Heap[T]) Stream added in v0.12.0

func (h *Heap[T]) Stream() *fun.Stream[T]

Stream provides an fun.Stream interface to the heap. The stream consumes items from the heap, and will return when the heap is empty.

type Item

type Item[T any] struct {
	// contains filtered or unexported fields
}

Item is a common wrapper for the elements in a stack.

func NewItem

func NewItem[T any](in T) *Item[T]

NewItem produces a valid Item object for the specified value.

func (*Item[T]) Append

func (it *Item[T]) Append(n *Item[T]) *Item[T]

Append inserts a new item after the following item in the stack, returning the new item, or if the new item is not valid, the item itself.

func (*Item[T]) Attach

func (it *Item[T]) Attach(stack *Stack[T]) bool

Attach removes items from the back of the stack and appends them to the current item. This inverts the order of items in the input stack.

func (*Item[T]) Detach

func (it *Item[T]) Detach() *Stack[T]

Detach splits a stack into two, using the current Item as the head of the new stack. The output is always non-nil: if the item is not valid or not the member of a stack Detach creates a new empty stack. If this item is currently the head of a stack, Detach returns that stack.

func (*Item[T]) In

func (it *Item[T]) In(s *Stack[T]) bool

In reports if an item is a member of a stack. Because item's track references to the stack, this is an O(1) operation.

func (*Item[T]) MarshalJSON

func (it *Item[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns the result of json.Marshal on the value of the item. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Item[T]) Next

func (it *Item[T]) Next() *Item[T]

Next returns the following item in the stack.

func (*Item[T]) Ok

func (it *Item[T]) Ok() bool

Ok return true if the Value() has been set. Returns false for incompletely initialized values.

Returns false when the item is nil.

func (*Item[T]) Remove

func (it *Item[T]) Remove() bool

Remove removes the item from the stack, (at some expense, for items deeper in the stack.) If the operation isn't successful or possible the operation returns false.

func (*Item[T]) Set

func (it *Item[T]) Set(v T) bool

Set mutates the value of an Item, returning true if the operation has been successful. The operation fails if the Item is the head item in a stack or not a member of a stack.

func (*Item[T]) String

func (it *Item[T]) String() string

String implements fmt.Stringer, and returns the string value of the item's value.

func (*Item[T]) UnmarshalJSON

func (it *Item[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads the json value, and sets the value of the item to the value in the json, potentially overriding an existing value. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Item[T]) Value

func (it *Item[T]) Value() T

Value returns the Item's underlying value. Use the Ok method to check the validity of the zero values.

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

List provides a doubly linked list. Callers are responsible for their own concurrency control and bounds checking, and should generally use with the same care as a slice.

The Deque implementation in the pubsub package provides a similar implementation with locking and a notification system.

func (*List[T]) Append

func (l *List[T]) Append(items ...T)

Append adds a variadic sequence of items to the end of the list.

func (*List[T]) Back

func (l *List[T]) Back() *Element[T]

Back returns a pointer to the last element of the list. If the list is empty, this is also the first element of the list. The operation is non-destructive. You can use this pointer to begin a c-style iteration over the list:

for e := list.Back(); e.Ok(); e = e.Previous() {
       // operate
}

func (*List[T]) Copy

func (l *List[T]) Copy() *List[T]

Copy duplicates the list. The element objects in the list are distinct, though if the Values are themselves references, the values of both lists would be shared.

func (*List[T]) Extend

func (l *List[T]) Extend(input *List[T])

Extend removes items from the front of the input list, and appends them to the end (back) of the current list.

func (*List[T]) Front

func (l *List[T]) Front() *Element[T]

Front returns a pointer to the first element of the list. If the list is empty, this is also the last element of the list. The operation is non-destructive. You can use this pointer to begin a c-style iteration over the list:

for e := list.Front(); e.Ok(); e = e.Next() {
       // operate
}

func (*List[T]) GeneratorBack added in v0.12.0

func (l *List[T]) GeneratorBack() fun.Generator[T]

GeneratorBack provides the same semantics and operation as the Generator operation, but starts at the end/tail of the list and works forward.

func (*List[T]) GeneratorFront added in v0.12.0

func (l *List[T]) GeneratorFront() fun.Generator[T]

GeneratorFront provides a generator function that iterates through the contents of the list, front-to-back. When the generator has fully iterated through the list, or iterates to an item that has been removed (likely due to concurrent access,) the generator returns io.EOF.

The GeneratorFront starts at the front of the list and iterates in order.

func (*List[T]) GeneratorPopBack added in v0.12.0

func (l *List[T]) GeneratorPopBack() fun.Generator[T]

GeneratorPopBack provides the same semantics and operation as the GeneratorPopBack operation, but starts at the end/tail of the list and works forward.

func (*List[T]) GeneratorPopFront added in v0.12.0

func (l *List[T]) GeneratorPopFront() fun.Generator[T]

GeneratorPopFront provides a generator function that iterates through the contents of the list, removing each item from the list as it encounters it. When the generator reaches has fully iterated through the list, or iterates to an item that has been removed (likely due to concurrent access,) the generator returns io.EOF.

In most cases, for destructive iteration, use the pubsub.Queue, pubsub.Deque, or one of the pubsub.Distributor implementations.

The generator starts at the front of the list and iterates in order.

func (*List[T]) IsSorted

func (l *List[T]) IsSorted(lt cmp.LessThan[T]) bool

IsSorted reports if the list is sorted from low to high, according to the LessThan function.

func (*List[T]) Iterator

func (l *List[T]) Iterator() iter.Seq[T]

Iterator returns a native go stream function for the items in a list.

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the length of the list. As the Append/Remove operations track the length of the list, this is an O(1) operation.

func (*List[T]) MarshalJSON

func (l *List[T]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON array representing the items in the list. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*List[T]) PopBack

func (l *List[T]) PopBack() *Element[T]

PopBack removes the last element from the list. If the list is empty, this returns a detached non-nil value, that will report an Ok() false value. You can use this element to produce a C-style stream over the list, that removes items during the iteration:

for e := list.PopBack(); e.Ok(); e = input.PopBack() {
	// do work
}

func (*List[T]) PopFront

func (l *List[T]) PopFront() *Element[T]

PopFront removes the first element from the list. If the list is empty, this returns a nil value, that will report an Ok() false You can use this element to produce a C-style stream over the list, that removes items during the iteration:

for e := list.PopFront(); e.Ok(); e = input.PopFront() {
	// do work
}

func (*List[T]) Populate added in v0.10.4

func (l *List[T]) Populate(iter *fun.Stream[T]) fun.Worker

Populate returns a worker that adds items from the stream to the list. Any error returned is either a context cancellation error or the result of a panic in the input stream. The close method on the input stream is not called.

func (*List[T]) PushBack

func (l *List[T]) PushBack(it T)

PushBack creates an element and appends it to the list. The performance of PushFront and PushBack are the same.

func (*List[T]) PushFront

func (l *List[T]) PushFront(it T)

PushFront creates an element and prepends it to the list. The performance of PushFront and PushBack are the same.

func (*List[T]) Slice added in v0.10.3

func (l *List[T]) Slice() Slice[T]

Slice exports the contents of the list to a slice.

func (*List[T]) SortMerge

func (l *List[T]) SortMerge(lt cmp.LessThan[T])

SortMerge sorts the list, using the provided comparison function and a Merge Sort operation. This is something of a novelty in most cases, as removing the elements from the list, adding to a slice and then using sort.Slice() from the standard library, and then re-adding those elements to the list, will perform better.

The operation will modify the input list, replacing it with an new list operation.

func (*List[T]) SortQuick

func (l *List[T]) SortQuick(lt cmp.LessThan[T])

SortQuick sorts the list, by removing the elements, adding them to a slice, and then using sort.SliceStable(). In many cases this performs better than the merge sort implementation.

func (*List[T]) StreamBack added in v0.12.0

func (l *List[T]) StreamBack() *fun.Stream[T]

StreamBack returns a stream that produces elements from the list, from the back to the front. The stream is not synchronized with the values in the list, and will be exhausted when you reach the front of the list.

If you add values to the list during iteration *behind* where the stream is, these values will not be present in the stream; however, values added ahead of the stream, will be visible.

func (*List[T]) StreamFront added in v0.12.0

func (l *List[T]) StreamFront() *fun.Stream[T]

StreamFront returns a stream over the values in the list in front-to-back order. The Stream is not synchronized with the values in the list, and will be exhausted when you reach the end of the list.

If you add values to the list during iteration *behind* where the stream is, these values will not be present in the stream; however, values added ahead of the stream, will be visible.

func (*List[T]) StreamPopBack added in v0.12.0

func (l *List[T]) StreamPopBack() *fun.Stream[T]

StreamPopBack produces a stream that consumes elements from the list as it iterates, moving back-to-front.

If you add values to the list during iteration *behind* where the stream is, these values will not be present in the stream; however, values added ahead of the stream, will be visible.

func (*List[T]) StreamPopFront added in v0.12.0

func (l *List[T]) StreamPopFront() *fun.Stream[T]

PopStream produces a stream that consumes elements from the list as it iterates, moving front-to-back.

If you add values to the list during iteration *behind* where the stream is, these values will not be present in the stream; however, values added ahead of the stream, will be visible.

func (*List[T]) UnmarshalJSON

func (l *List[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads json input and adds that to values in the list. If there are elements in the list, they are not removed. By supporting json.Marshaler and json.Unmarshaler, Elements and lists can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

type Map

type Map[K comparable, V any] map[K]V

Map is just a generic type wrapper around a map, mostly for the purpose of being able to interact with Pair[K,V] objects and Streams.

All normal map operations are still accessible, these methods exist to provide accessible function objects for use in contexts where that may be useful and to improve the readability of some call sites, where default map access may be awkward.

func NewMap added in v0.10.7

func NewMap[K comparable, V any](in map[K]V) Map[K, V]

NewMap provides a constructor that will produce a fun.Map without specifying types.

func (Map[K, V]) Add

func (m Map[K, V]) Add(k K, v V)

Add adds a key value pair directly to the map.

func (Map[K, V]) AddPair

func (m Map[K, V]) AddPair(p Pair[K, V])

AddPair adds a Pair holding K and V objects to the map.

func (Map[K, V]) AddTuple added in v0.10.5

func (m Map[K, V]) AddTuple(p Tuple[K, V])

AddTuple adds a Tuple of K and V objects to the map.

func (Map[K, V]) Append

func (m Map[K, V]) Append(pairs ...Pair[K, V])

Append adds a sequence of Pair objects to the map.

func (Map[K, V]) Check

func (m Map[K, V]) Check(key K) bool

Check returns true if the value K is in the map.

func (Map[K, V]) Delete added in v0.10.8

func (m Map[K, V]) Delete(k K)

Delete removes a key from the map.

func (Map[K, V]) Extend

func (m Map[K, V]) Extend(pairs *Pairs[K, V])

Extend adds a sequence of Pairs to the map.

func (Map[K, V]) ExtendWithPairs added in v0.12.0

func (m Map[K, V]) ExtendWithPairs(pairs *Pairs[K, V])

ExtendWithPairs adds items to the map from a Pairs object. Existing values for K are always overwritten.

func (Map[K, V]) ExtendWithStream added in v0.12.0

func (m Map[K, V]) ExtendWithStream(it *fun.Stream[Pair[K, V]]) fun.Worker

ExtendWithStream adds items to the map. If a key already exists in the map, it will be overwritten for subsequent appearances of that key.

This operation is lazy, and returns a Worker (future) function that must be excuted to process the stream.

func (Map[K, V]) ExtendWithTuples added in v0.12.0

func (m Map[K, V]) ExtendWithTuples(tuples *Tuples[K, V])

ExtendWithTuples adds items to the map from a Tuples object. Existing values for K are always overwritten.

func (Map[K, V]) Generator added in v0.12.0

func (m Map[K, V]) Generator() fun.Generator[Pair[K, V]]

Generator constructs a fun.Generator function for the pairs in the map. The operation starts a goroutine on the first iteration that tracks the state of the stream. Iteration order is randomized.

func (Map[K, V]) GeneratorKeys added in v0.12.0

func (m Map[K, V]) GeneratorKeys() fun.Generator[K]

GeneratorKeys returns a generator that generates the keys of the map. The operation requires a goroutine to keep track of the state of the iteration, but does not buffer or cache keys.

func (Map[K, V]) GeneratorValues added in v0.12.0

func (m Map[K, V]) GeneratorValues() fun.Generator[V]

GeneratorValues returns a generator that generates the values of the map. The operation requires a goroutine to keep track of the state of the iteration, but does not buffer or cache values.

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) V

Get returns the value from the map, and is the same thing as:

foo := mp[key]

If the key is not present in the map, as with a normal map, this is the zero value for V.

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() *fun.Stream[K]

Keys provides a stream over just the keys in the map.

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

Len returns the length. It is equivalent to len(Map), but is provided for consistency.

func (Map[K, V]) Load

func (m Map[K, V]) Load(key K) (V, bool)

Load returns the value in the map for the key, and an "ok" value which is true if that item is present in the map.

func (Map[K, V]) Pairs

func (m Map[K, V]) Pairs() *Pairs[K, V]

Pairs exports a Pairs object, containing the contents of the map.

func (Map[K, V]) SetDefault

func (m Map[K, V]) SetDefault(key K)

SetDefault set's sets the provided key in the map to the zero value for the value type.

func (Map[K, V]) Store added in v0.12.0

func (m Map[K, V]) Store(k K, v V)

Store adds a key value pair directly to the map.

func (Map[K, V]) Stream added in v0.12.0

func (m Map[K, V]) Stream() *fun.Stream[Pair[K, V]]

Stream converts a map into a stream of dt.Pair objects. The stream is panic-safe, and uses one go routine to track the progress through the map. As a result you should always, either exhaust the stream, cancel the context that you pass to the stream OR call stream.Close().

To use this stream the items in the map are not copied, and the iteration order is randomized following the convention in go.

Use in combination with other stream processing tools (generators, observers, transformers, etc.) to limit the number of times a collection of data must be coppied.

func (Map[K, V]) Tuples added in v0.10.5

func (m Map[K, V]) Tuples() *Tuples[K, V]

Tuples exports a map a Pairs object, containing the contents of the map.

func (Map[K, V]) Values

func (m Map[K, V]) Values() *fun.Stream[V]

Values provides a stream over just the values in the map.

type Optional added in v0.10.5

type Optional[T any] struct {
	// contains filtered or unexported fields
}

Optional is a wrapper type for optional value, where using a pointer is unsuitable or awkward. The type provides a reasonable interface for manipulating the value, does not need to be initialized upon construction (i.e. safe to put in structs without needing to specify an initial value.) MarshalText and MarshalBinary (with corresponding) unmarshal) interfaces make Optional types easy to embed.

Marshal/Unmarshal methods are provided to support using optional in primary structs and to avoid the need to duplicate structs.

func NewOptional added in v0.10.5

func NewOptional[T any](in T) Optional[T]

NewOptional is simple constructor that constructs a new populated Optional value.

func (*Optional[T]) Default added in v0.10.5

func (o *Optional[T]) Default(in T)

Default sets value of the optional to the provided value if it is not already been defined.

func (*Optional[T]) Future added in v0.10.5

func (o *Optional[T]) Future() fn.Future[T]

Future provides access to the value of the optional as a Future function. This does not disambiguate zero values. Use in conjunction with Optional.Handler and adt.AccessorsWithLock and adt.AccessorsWithReadLock to handle concurency control.

func (*Optional[T]) Get added in v0.10.5

func (o *Optional[T]) Get() (T, bool)

Get returns the current value of the optional and the ok value. Use this to disambiguate zero values.

func (*Optional[T]) Handler added in v0.10.5

func (o *Optional[T]) Handler() fn.Handler[T]

Handler provides access to setting the optional value as fn.Handler function. Use in conjunction with Optional.Future and adt.AccessorsWithLock and adt.AccessorsWithReadLock to handle concurency control.

func (Optional[T]) MarshalBinary added in v0.10.5

func (o Optional[T]) MarshalBinary() ([]byte, error)

MarshalBinary supports marshaling optional values into binary formats and uses the value of the optional to dictate behavior. The underlying value must implement encoding.BinaryMarshaler, bson.Marshaler or a generic Marshal() interface. byte slices are passed through.

func (Optional[T]) MarshalText added in v0.10.5

func (o Optional[T]) MarshalText() ([]byte, error)

MarshalText defines how to marshal the optional value in text-based contexts. In most cases it falls back on the value of the optional: using encoding.TextMarshaler, json.Marshaler, yaml.Marahaler, or a generic Marshal() ([]byte,error) interface are called. Strings and []byte values are through directly, and failing all of these options, this falls back to json.Marshal().

func (Optional[T]) Ok added in v0.10.9

func (o Optional[T]) Ok() bool

Ok returns true when the optional

func (*Optional[T]) Reset added in v0.10.5

func (o *Optional[T]) Reset()

Reset unsets the OK value of the optional, and unsets the reference to the existing value.

func (*Optional[T]) Resolve added in v0.10.5

func (o *Optional[T]) Resolve() T

Resolve returns the current value of the optional. Zero values of T are ambiguous.

func (*Optional[T]) Scan added in v0.10.5

func (o *Optional[T]) Scan(src any) (err error)

Scan implements the sql.Scanner interface. This is invalid if the type of the optional value is not a primitive value type.

func (*Optional[T]) Set added in v0.10.5

func (o *Optional[T]) Set(in T)

Set marks the optional value as defined, and sets the optional value. You can set an optional to the zero value for type T. To "unset" a value use the Reset().

func (*Optional[T]) Swap added in v0.10.5

func (o *Optional[T]) Swap(next T) (prev T)

Swap returns the previous value of the optional and replaces it with the provided value

func (*Optional[T]) UnmarshalBinary added in v0.10.5

func (o *Optional[T]) UnmarshalBinary(in []byte) (err error)

UnmarshalBinary provides a compliment to MarshalBinary, and serves as a passthrough for encoding.BinaryUnmarshaler, bson.Unmarshaler and the generic Unmarshal interface.

func (*Optional[T]) UnmarshalText added in v0.10.5

func (o *Optional[T]) UnmarshalText(in []byte) (err error)

UnmarshalText provides an inverse version of MarshalText for the encoding.UnmarshalText interface. encoding.TextUnmarshaler, json.Unmarshaler, yaml.Unmarshaler, and a generic Unmarshler interface. Strings and bytes slices pass through directly, and json.Unmarshal() is used in all other situations.

func (Optional[T]) Value added in v0.10.5

func (o Optional[T]) Value() (driver.Value, error)

Value implements the SQL driver.Valuer interface. Scan implements the sql Scanner interface. This is invalid if the type of the optional value is not a primitive value type.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Pair represents a key-value pair. Used by the adt synchronized map implementation and the set package to handle ordered key-value pairs.

func MakePair

func MakePair[K comparable, V any](k K, v V) Pair[K, V]

MakePair constructs a pair object. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.

func (Pair[K, V]) Get added in v0.11.0

func (p Pair[K, V]) Get() (K, V)

Get returns only the key and value of a Pair. Provided for symmetry.

func (Pair[K, V]) GetKey added in v0.11.0

func (p Pair[K, V]) GetKey() K

GetValue returns only the key of a Pair. Provided for symmetry.

func (Pair[K, V]) GetValue added in v0.11.0

func (p Pair[K, V]) GetValue() V

GetValue returns only the value of a Pair. Provided for symmetry.

func (*Pair[K, V]) Set added in v0.11.0

func (p *Pair[K, V]) Set(k K, v V) *Pair[K, V]

Set is a helper to modify the both values of a pair. This method can be chained.

func (*Pair[K, V]) SetKey added in v0.11.0

func (p *Pair[K, V]) SetKey(k K) *Pair[K, V]

SetKey modifies the key of a pair. This method can be chained.

func (*Pair[K, V]) SetValue added in v0.11.0

func (p *Pair[K, V]) SetValue(v V) *Pair[K, V]

SetValue modifies the value of a pair. This method can be chained.

type Pairs

type Pairs[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Pairs implements a collection of key-value pairs.

func MakePairs

func MakePairs[K comparable, V any](in ...Pair[K, V]) *Pairs[K, V]

MakePairs constructs a Pairs object from a sequence of Pairs. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.

To build Pairs objects from other types, use the Consume methods.

func (*Pairs[K, V]) Add

func (p *Pairs[K, V]) Add(k K, v V) *Pairs[K, V]

Add adds a new value to the underlying slice. This may add a duplicate key. The return value is provided to support chaining Add() operations

func (*Pairs[K, V]) Append

func (p *Pairs[K, V]) Append(vals ...Pair[K, V])

Append as a collection of pairs to the collection of key/value pairs.

func (*Pairs[K, V]) Consume

func (p *Pairs[K, V]) Consume(iter *fun.Stream[Pair[K, V]]) fun.Worker

Consume adds items from a stream of pairs to the current Pairs slice.

func (*Pairs[K, V]) ConsumeMap

func (p *Pairs[K, V]) ConsumeMap(in map[K]V)

ConsumeMap adds all of the items in a map to the Pairs object.

func (*Pairs[K, V]) ConsumeSlice

func (p *Pairs[K, V]) ConsumeSlice(in []V, keyf func(V) K)

ConsumeSlice adds all the values from the input slice to the Pairs object, creating the keys using the function provide.

func (*Pairs[K, V]) ConsumeValues

func (p *Pairs[K, V]) ConsumeValues(iter *fun.Stream[V], keyf func(V) K) fun.Worker

ConsumeValues adds all of the values from the input stream, generating the keys using the function provided.

func (*Pairs[K, V]) Copy added in v0.10.3

func (p *Pairs[K, V]) Copy() *Pairs[K, V]

Copy produces a new Pairs object with the same values.

func (*Pairs[K, V]) Extend

func (p *Pairs[K, V]) Extend(toAdd *Pairs[K, V])

Extend adds the items from a Pairs object (slice of Pair) without modifying the donating object.

func (*Pairs[K, V]) Iterator

func (p *Pairs[K, V]) Iterator() iter.Seq[Pair[K, V]]

Iterator returns a native go iterator function for pair items.

func (*Pairs[K, V]) Iterator2 added in v0.12.0

func (p *Pairs[K, V]) Iterator2() iter.Seq2[K, V]

Iterator2 returns a native go iterator function for the items in a pairs sequence.

func (*Pairs[K, V]) Keys

func (p *Pairs[K, V]) Keys() *fun.Stream[K]

Keys returns a stream over only the keys in a sequence of iterator items.

func (*Pairs[K, V]) Len

func (p *Pairs[K, V]) Len() int

Len returns the number of items in the pairs object.

func (*Pairs[K, V]) List added in v0.10.2

func (p *Pairs[K, V]) List() *List[Pair[K, V]]

List returns the sequence of pairs as a list.

func (*Pairs[K, V]) Map

func (p *Pairs[K, V]) Map() Map[K, V]

Map converts a list of pairs to the equivalent map. If there are duplicate keys in the Pairs list, only the first occurrence of the key is retained.

func (*Pairs[K, V]) MarshalJSON

func (p *Pairs[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON encoding for the Pairs object by first converting it to a map and then encoding that map as JSON. The JSON serialization does not necessarily preserve the order of the pairs object.

func (*Pairs[K, V]) Push added in v0.10.5

func (p *Pairs[K, V]) Push(pair Pair[K, V])

Push adds a single pair to the slice of pairs. This may add a duplicate key.

func (*Pairs[K, V]) ReadAll added in v0.12.0

func (p *Pairs[K, V]) ReadAll(pf fun.Handler[Pair[K, V]]) fun.Worker

ReadAll returns a worker, that when executed calls the processor function for every pair in the container.

func (*Pairs[K, V]) Slice

func (p *Pairs[K, V]) Slice() []Pair[K, V]

Slice creates a new slice of all the Pair objects.

func (*Pairs[K, V]) SortMerge added in v0.10.2

func (p *Pairs[K, V]) SortMerge(c cmp.LessThan[Pair[K, V]])

SortMerge performs a merge sort on the collected pairs.

func (*Pairs[K, V]) SortQuick added in v0.10.2

func (p *Pairs[K, V]) SortQuick(c cmp.LessThan[Pair[K, V]])

SortQuick does a quick sort using sort.StableSort. Typically faster than SortMerge, but potentially more memory intensive for some types.

func (*Pairs[K, V]) Stream added in v0.12.0

func (p *Pairs[K, V]) Stream() *fun.Stream[Pair[K, V]]

Stream return a stream over each key-value pairs.

func (*Pairs[K, V]) UnmarshalJSON

func (p *Pairs[K, V]) UnmarshalJSON(in []byte) error

UnmarshalJSON provides consistent JSON decoding for Pairs objects. It reads a JSON document into a map and converts it to pairs, and appends it to the existing Pairs objects without removing or resetting the current object.

func (*Pairs[K, V]) Values

func (p *Pairs[K, V]) Values() *fun.Stream[V]

Values returns a stream over only the values in a sequence of iterator pairs.

type Ring added in v0.12.0

type Ring[T any] struct {
	// contains filtered or unexported fields
}

Ring is a simple generic ring-buffer implemented on top of a slice/array with a few conveniences: there are forward and backward streams; you can pop items from the "end" (oldest) in the buffer. The Total() method maintains a count of the total number of items added to the buffer.

Operations on the Ring are NOT safe for concurrent use from multiple go routines.

func (*Ring[T]) Cap added in v0.12.0

func (r *Ring[T]) Cap() int

Returns the capacity of the ring buffer. This is either the size passed to Setup() or the default 1024.

func (*Ring[T]) FIFO added in v0.12.0

func (r *Ring[T]) FIFO() *fun.Stream[T]

FIFO returns a stream that begins at the first (oldest; Head) element and streams forward to the current or most recently added element in the buffer.

func (*Ring[T]) Head added in v0.12.0

func (r *Ring[T]) Head() T

Head returns the oldest element in the buffer.

func (*Ring[T]) LIFO added in v0.12.0

func (r *Ring[T]) LIFO() *fun.Stream[T]

LIFO returns the element that was most recently added to buffer and iterates backwords to the oldest element in the buffer.

func (*Ring[T]) Len added in v0.12.0

func (r *Ring[T]) Len() int

Len returns the number of elements in the buffer. This is, some number between the capacity and 0. It is decremented when items are popped from the buffer, but only incremented when a previously empty position is filled.

func (*Ring[T]) Pop added in v0.12.0

func (r *Ring[T]) Pop() *T

Pop returns the oldest element in the buffer to the caller. If the buffer is empty, then Pop() returns nil.

The returned value is (effectively) owned by the caller of Pop() and is independent of the value stored in the ring.

func (*Ring[T]) PopFIFO added in v0.12.0

func (r *Ring[T]) PopFIFO() *fun.Stream[T]

PopFIFO returns a FIFO stream that consumes elements in the buffer, starting with the oldest element in the buffer and moving through all elements. The stream is exhusted when the buffer is empty.

func (*Ring[T]) Push added in v0.12.0

func (r *Ring[T]) Push(val T)

Push adds an element to the buffer in the next position, potentially overwriting the oldest element in the buffer once the buffer is full.

Elements are always pushed to the "next" position in the buffer, even if elements are removed using Pop().

func (*Ring[T]) Setup added in v0.12.0

func (r *Ring[T]) Setup(size int)

Setup sets the size of the ring buffer and initializes the buffer, if the buffer hasn't been used. Using the buffer initializes it with a size of 1024.

func (*Ring[T]) Tail added in v0.12.0

func (r *Ring[T]) Tail() T

Tail returns the newest (or most recently added) element in the buffer.

func (*Ring[T]) Total added in v0.12.0

func (r *Ring[T]) Total() uint64

Total returns the number of elements that have ever been added to the buffer. This number is never decremented.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set provides a flexible generic set implementation, with optional safety for concurrent use (via Synchronize() and WithLock() methods,) and optional order tracking (via Order().)

func NewSetFromMap added in v0.10.3

func NewSetFromMap[K, V comparable](in map[K]V) *Set[Pair[K, V]]

NewSetFromMap constructs a Set of pairs derived from the input map. The resulting set is constrained by both the keys and the values, and so would permit duplicate "keys" from the perspective of the map, and may not therefore roundtrip.

func NewSetFromSlice added in v0.10.3

func NewSetFromSlice[T comparable](in []T) *Set[T]

NewSetFromSlice constructs a map and adds all items from the input slice to the new set.

func (*Set[T]) Add

func (s *Set[T]) Add(in T)

Add attempts to add the item to the mutex, and is a noop otherwise.

func (*Set[T]) AddCheck

func (s *Set[T]) AddCheck(in T) (ok bool)

AddCheck adds an item to the set and returns true if the item had been in the set before AddCheck. In all cases when AddCheck returns, the item is a member of the set.

func (*Set[T]) AppendSet added in v0.12.0

func (s *Set[T]) AppendSet(extra *Set[T])

AppendSet adds the items of one set to this set.

func (*Set[T]) AppendStream added in v0.12.0

func (s *Set[T]) AppendStream(iter *fun.Stream[T])

AppendStream adds all items encountered in the stream to the set.

func (*Set[T]) Check

func (s *Set[T]) Check(in T) bool

Check returns true if the item is in the set.

func (*Set[T]) Delete

func (s *Set[T]) Delete(in T)

Delete attempts to remove the item from the set.

func (*Set[T]) DeleteCheck

func (s *Set[T]) DeleteCheck(in T) bool

DeleteCheck removes the item from the set, return true when the item had been in the Set, and returning false othewise

func (*Set[T]) Equal

func (s *Set[T]) Equal(other *Set[T]) bool

Equal tests two sets, returning true if the items in the sets have equal values. If the sets are ordered, order is considered.

func (*Set[T]) Generator added in v0.12.0

func (s *Set[T]) Generator() (out fun.Generator[T])

Generator will produce each item from set on successive calls. If the Set is ordered, then the generator produces items in the set's order. If the Set is synchronize, then the Generator always holds the Set's lock when called.

func (*Set[T]) Iterator

func (s *Set[T]) Iterator() iter.Seq[T]

Iterator returns a new-style native Go iterator for the items in the set.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of items tracked in the set.

func (*Set[T]) MarshalJSON

func (s *Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON generates a JSON array of the items in the set.

func (*Set[T]) Order

func (s *Set[T]) Order()

Order enables order tracking for the Set. The method panics if there is more than one item in the map. If order tracking is enabled, this operation is a noop.

func (*Set[T]) SortMerge added in v0.10.4

func (s *Set[T]) SortMerge(lt cmp.LessThan[T])

SortMerge sorts the elements in an ordered set using a merge sort algorithm. If the set is not ordered, it will become ordered. Unlike the Order() method, you can use this method on a populated but unordered set.

func (*Set[T]) SortQuick added in v0.10.4

func (s *Set[T]) SortQuick(lt cmp.LessThan[T])

SortQuick sorts the elements in the set using sort.StableSort. Typically faster than SortMerge, but potentially more memory intensive for some types. If the set is not ordered, it will become ordered. Unlike the Order() method, you can use this method on a populated but unordered set.

func (*Set[T]) Stream added in v0.12.0

func (s *Set[T]) Stream() *fun.Stream[T]

Stream provides a way to iterate over the items in the set. Provides items in iteration order if the set is ordered.

func (*Set[T]) Synchronize

func (s *Set[T]) Synchronize()

Synchronize creates a mutex and enables its use in the Set. This operation is safe to call more than once,

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads input JSON data, constructs an array in memory and then adds items from the array to existing set. Items that are in the set when UnmarshalJSON begins are not modified.

func (*Set[T]) WithLock

func (s *Set[T]) WithLock(mtx *sync.Mutex)

WithLock configures the Set to synchronize operations with this mutex. If the mutex is nil, or the Set is already synchronized with a different mutex, WithLock panics with an invariant violation.

type Slice

type Slice[T any] []T

Slice is just a local wrapper around a slice, providing a similarly expressive interface to the Map and Pair types in the fun package.

func DefaultSlice added in v0.10.4

func DefaultSlice[T any](input []T, args ...int) Slice[T]

DefaultSlice takes a slice value and returns it if it's non-nil. If the slice is nil, it returns a slice of the specified length (and capacity,) as specified.

func MergeSlices added in v0.10.5

func MergeSlices[T any](sls ...[]T) Slice[T]

MergeSlices takes a variadic set of arguments which are all slices, and returns a single slice, that contains all items in the input slice.

func NewSlice added in v0.10.7

func NewSlice[T any](in []T) Slice[T]

NewSlice produces a slice object as a convenience constructor to avoid needing to specify types.

func SlicePtrs added in v0.10.4

func SlicePtrs[T any](in []T) Slice[*T]

SlicePtrs converts a slice of values to a slice of values. This is a helper for NewSlice(in).Ptrs().

func SliceRefs added in v0.10.4

func SliceRefs[T any](in []*T) Slice[T]

SliceRefs converts a slice of pointers to a slice of values, replacing all nil pointers with the zero type for that value.

func SliceSparseRefs added in v0.10.4

func SliceSparseRefs[T any](in []*T) Slice[T]

SliceSparseRefs converts a slice of pointers to a slice of objects, dropping nil values. from the output slice.

func Variadic

func Variadic[T any](in ...T) Slice[T]

Variadic constructs a slice of type T from a sequence of variadic elements.

func (*Slice[T]) Add

func (s *Slice[T]) Add(in T)

Add adds a single item to the end of the slice.

func (*Slice[T]) Append

func (s *Slice[T]) Append(in ...T)

Append adds all of the items to the end of the slice.

func (Slice[T]) Cap

func (s Slice[T]) Cap() int

Cap returns the capacity of the slice.

func (Slice[T]) Copy

func (s Slice[T]) Copy() Slice[T]

Copy performs a shallow copy of the Slice.

func (*Slice[T]) Empty

func (s *Slice[T]) Empty()

Empty re-slices the slice, to omit all items, but retain the allocation.

func (*Slice[T]) Extend

func (s *Slice[T]) Extend(in []T)

Extend adds the items from the input slice to the root slice.

func (Slice[T]) FillTo added in v0.10.5

func (s Slice[T]) FillTo(length int) Slice[T]

FillTo appends zero values to the slice until it reaches the specified length, and returns the resulting slice.

func (*Slice[T]) Filter

func (s *Slice[T]) Filter(p func(T) bool) (o Slice[T])

Filter returns a new slice, having passed all the items in the input slice. Items that the filter function returns true for are included and others are skipped.

func (*Slice[T]) FilterFuture

func (s *Slice[T]) FilterFuture(p func(T) bool) fn.Future[Slice[T]]

FilterFuture returns a future that generates a new slice using the filter to select items from the root slice.

func (*Slice[T]) Grow added in v0.10.4

func (s *Slice[T]) Grow(size int)

Grow adds zero items to the slice until it reaches the desired size.

func (*Slice[T]) GrowCapacity added in v0.10.5

func (s *Slice[T]) GrowCapacity(size int)

GrowCapacity extends the capacity of the slice (by adding zero items and the )

func (Slice[T]) Index added in v0.10.4

func (s Slice[T]) Index(index int) T

Index returns the item at the specified index.

If the provided index is not within the bounds of the slice the operation panics.

func (Slice[T]) IsEmpty

func (s Slice[T]) IsEmpty() bool

IsEmpty returns true when there are no items in the slice (or it is nil.

func (Slice[T]) Last

func (s Slice[T]) Last() int

Last returns the index of the last element in the slice. Empty slices have `-1` last items.

func (Slice[T]) Len

func (s Slice[T]) Len() int

Len returns the length of the slice.

func (*Slice[T]) Populate added in v0.10.3

func (s *Slice[T]) Populate(iter *fun.Stream[T]) fun.Worker

Populate constructs an operation that adds all items from the stream to the slice.

func (*Slice[T]) Prepend added in v0.10.4

func (s *Slice[T]) Prepend(in ...T)

Prepend adds the items to beginning of the slice.

func (Slice[T]) Ptr added in v0.10.2

func (s Slice[T]) Ptr(index int) *T

Ptr provides a pointer to the item at the provided index.

func (Slice[T]) Ptrs added in v0.10.2

func (s Slice[T]) Ptrs() []*T

Ptrs converts a slice in to a slice of pointers to the values in the original slice

func (Slice[T]) ReadAll added in v0.12.0

func (s Slice[T]) ReadAll(of fn.Handler[T])

ReadAll calls the observer function on every item in the slice.

func (*Slice[T]) Reset

func (s *Slice[T]) Reset()

Reset constructs a new empty slice releasing the original allocation.

func (*Slice[T]) Reslice

func (s *Slice[T]) Reslice(start, end int)

Reslice modifies the slice to set the new start and end indexes.

Slicing operations, can lead to panics if the indexes are out of bounds.

func (*Slice[T]) ResliceBeginning

func (s *Slice[T]) ResliceBeginning(start int)

ResliceBeginning moves the "beginning" of the slice to the specified index.

Slicing operations, can lead to panics if the indexes are out of bounds.

func (*Slice[T]) ResliceEnd

func (s *Slice[T]) ResliceEnd(end int)

ResliceEnd moves the "end" of the slice to the specified index.

Slicing operations, can lead to panics if the indexes are out of bounds.

func (Slice[T]) Sort

func (s Slice[T]) Sort(cp func(a, b T) bool)

Sort reorders the slice using the provided com parator function, which should return true if a is less than b and, false otherwise. The slice is sorted in "lowest" to "highest" order.

func (Slice[T]) Sparse added in v0.10.5

func (s Slice[T]) Sparse() Slice[T]

Sparse always returns a new slice. It iterates through the elements in the source slice and checks, using ft.IsNil() (which uses reflection), if the value is nil, and only adds the item when it is not-nil.

func (Slice[T]) Stream added in v0.12.0

func (s Slice[T]) Stream() *fun.Stream[T]

Stream returns a stream to the items of the slice the range keyword also works for these slices.

func (*Slice[T]) Truncate

func (s *Slice[T]) Truncate(n int)

Truncate removes the last n items from the end of the list.

Slicing operations, can lead to panics if the indexes are out of bounds.

func (Slice[T]) Zero added in v0.10.5

func (s Slice[T]) Zero()

Zero replaces all items in the slice with the zero value for the type T.

func (Slice[T]) ZeroRange added in v0.10.5

func (s Slice[T]) ZeroRange(start, end int)

ZeroRange replaces values between the specified indexes (inclusive) with the zero value of the type. If the indexes provided are outside of the bounds of the slice, an invariant violation panic is raised.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack provides a generic singly linked list, with an interface that is broadly similar to dt.List.

func (*Stack[T]) Append

func (s *Stack[T]) Append(items ...T)

Append adds a variadic sequence of items to the list.

func (*Stack[T]) Generator added in v0.12.0

func (s *Stack[T]) Generator() fun.Generator[T]

func (*Stack[T]) GeneratorPop added in v0.12.0

func (s *Stack[T]) GeneratorPop() fun.Generator[T]

func (*Stack[T]) Head

func (s *Stack[T]) Head() *Item[T]

Head returns the item at the top of this stack. This is a non destructive operation.

func (*Stack[T]) Iterator

func (s *Stack[T]) Iterator() iter.Seq[T]

Iterator returns a native go iterator function for the items in a set.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the length of the stack. Because stack's track their own size, this is an O(1) operation.

func (*Stack[T]) MarshalJSON

func (s *Stack[T]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON array representing the items in the stack. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() *Item[T]

Pop removes the item on the top of the stack, and returns it. If the stack is empty, this will return, but not detach, the root item of the stack, which will report a false Ok() value.

func (*Stack[T]) Populate added in v0.10.4

func (s *Stack[T]) Populate(iter *fun.Stream[T]) fun.Worker

Populate returns a worker that adds items from the stream to the stack. Any error returned is either a context cancellation error or the result of a panic in the input stream. The close method on the input stream is not called.

func (*Stack[T]) Push

func (s *Stack[T]) Push(it T)

Push appends an item to the stack.

func (*Stack[T]) Stream added in v0.12.0

func (s *Stack[T]) Stream() *fun.Stream[T]

Stream returns a non-destructive stream over the Items in a stack. Stream will not observe new items added to the stack during iteration.

func (*Stack[T]) StreamPop added in v0.12.0

func (s *Stack[T]) StreamPop() *fun.Stream[T]

StreamPop returns a destructive stream over the Items in a stack. StreamPop will not observe new items added to the stack during iteration.

func (*Stack[T]) UnmarshalJSON

func (s *Stack[T]) UnmarshalJSON(in []byte) error

UnmarshalJSON reads json input and adds that to values in the stack. If there are items in the stack, they are not removed. By supporting json.Marshaler and json.Unmarshaler, Items and stacks can behave as arrays in larger json objects, and can be as the output/input of json.Marshal and json.Unmarshal.

type Tuple added in v0.10.5

type Tuple[K any, V any] struct {
	One K
	Two V
}

Tuple represents a key-value tuple. Used by the adt synchronized map implementation and the set package to handle ordered key-value tuples.

func MakeTuple added in v0.10.5

func MakeTuple[K any, V any](k K, v V) Tuple[K, V]

MakeTuple constructs a tuple object. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.

func (Tuple[K, V]) MarshalJSON added in v0.10.5

func (t Tuple[K, V]) MarshalJSON() ([]byte, error)

func (*Tuple[K, V]) UnmarshalJSON added in v0.10.5

func (t *Tuple[K, V]) UnmarshalJSON(in []byte) error

type Tuples added in v0.10.5

type Tuples[K any, V any] struct {
	// contains filtered or unexported fields
}

Tuples implements a collection of key-value tuples.

func MakeTuples added in v0.10.5

func MakeTuples[K any, V any](in ...Tuple[K, V]) *Tuples[K, V]

MakeTuples constructs a Tuples object from a sequence of Tuples. This is identical to using the literal constructor but may be more ergonomic as the compiler seems to be better at inferring types in function calls over literal constructors.

To build Tuples objects from other types, use the Consume methods.

func (*Tuples[K, V]) Add added in v0.10.5

func (p *Tuples[K, V]) Add(k K, v V) *Tuples[K, V]

Add adds a new value to the underlying slice. This may add a duplicate key. The return value is provided to support chaining Add() operations

func (*Tuples[K, V]) Append added in v0.10.5

func (p *Tuples[K, V]) Append(vals ...Tuple[K, V])

Append as a collection of tuples to the collection of key/value tuples.

func (*Tuples[K, V]) Consume added in v0.10.5

func (p *Tuples[K, V]) Consume(iter *fun.Stream[Tuple[K, V]]) fun.Worker

Consume adds items from a stream of tuples to the current Tuples slice.

func (*Tuples[K, V]) Copy added in v0.10.5

func (p *Tuples[K, V]) Copy() *Tuples[K, V]

Copy produces a new Tuples object with the same values.

func (*Tuples[K, V]) Extend added in v0.10.5

func (p *Tuples[K, V]) Extend(toAdd *Tuples[K, V])

Extend adds the items from a Tuples object (slice of Tuple) without modifying the donating object.

func (*Tuples[K, V]) Iterator added in v0.10.5

func (p *Tuples[K, V]) Iterator() iter.Seq[Tuple[K, V]]

Iterator returns a native go iterator function for tuples.

func (*Tuples[K, V]) Iterator2 added in v0.12.0

func (p *Tuples[K, V]) Iterator2() iter.Seq2[K, V]

Iterator2 returns a native go iterator over the items in a collections of tuples.

func (*Tuples[K, V]) Len added in v0.10.5

func (p *Tuples[K, V]) Len() int

Len returns the number of items in the tuples object.

func (*Tuples[K, V]) List added in v0.10.5

func (p *Tuples[K, V]) List() *List[Tuple[K, V]]

List returns the sequence of tuples as a list.

func (*Tuples[K, V]) MarshalJSON added in v0.10.5

func (p *Tuples[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON produces a JSON encoding for the Pairs object by first converting it to a map and then encoding that map as JSON. The JSON serialization does not necessarily preserve the order of the pairs object.

func (*Tuples[K, V]) Ones added in v0.10.5

func (p *Tuples[K, V]) Ones() *fun.Stream[K]

Ones returns a stream over only the first item from a sequence of tuples.

func (*Tuples[K, V]) Push added in v0.10.5

func (p *Tuples[K, V]) Push(tuple Tuple[K, V])

Push adds a single tuple to the slice of tuples. This may add a duplicate key.

func (*Tuples[K, V]) ReadAll added in v0.12.0

func (p *Tuples[K, V]) ReadAll(pf fun.Handler[Tuple[K, V]]) fun.Worker

ReadAll returns a worker, that when executed calls the processor function for every tuple in the container.

func (*Tuples[K, V]) Slice added in v0.10.5

func (p *Tuples[K, V]) Slice() []Tuple[K, V]

Slice creates a new slice of all the Tuple objects.

func (*Tuples[K, V]) SortMerge added in v0.10.5

func (p *Tuples[K, V]) SortMerge(c cmp.LessThan[Tuple[K, V]])

SortMerge performs a merge sort on the collected tuples.

func (*Tuples[K, V]) SortQuick added in v0.10.5

func (p *Tuples[K, V]) SortQuick(c cmp.LessThan[Tuple[K, V]])

SortQuick does a quick sort using sort.StableSort. Typically faster than SortMerge, but potentially more memory intensive for some types.

func (*Tuples[K, V]) Stream added in v0.12.0

func (p *Tuples[K, V]) Stream() *fun.Stream[Tuple[K, V]]

Stream return a stream over each key-value tuples.

func (*Tuples[K, V]) Twos added in v0.10.5

func (p *Tuples[K, V]) Twos() *fun.Stream[V]

Twos returns a stream over only the second item from a sequence of tuples.

func (*Tuples[K, V]) UnmarshalJSON added in v0.10.5

func (p *Tuples[K, V]) UnmarshalJSON(in []byte) error

UnmarshalJSON provides consistent JSON decoding for Pairs objects. It reads a JSON document into a map and converts it to pairs, and appends it to the existing Pairs objects without removing or resetting the current object.

Directories

Path Synopsis
Package cmp provides comparators for sorting linked lists.
Package cmp provides comparators for sorting linked lists.
Package hdrhist provides an implementation of Gil Tene's HDR Histogram data structure.
Package hdrhist provides an implementation of Gil Tene's HDR Histogram data structure.

Jump to

Keyboard shortcuts

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