ds

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ds contains data structures

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[T any] []T

func NewList

func NewList[T any](capacity int) List[T]

NewList creates an empty List with given capacity

func (List[T]) All

func (l List[T]) All(ok func(T) bool) bool

All checks if all List items pass the ok function

func (List[T]) AllIndexFunc

func (l List[T]) AllIndexFunc(itemFn func(T) bool) List[int]

AllIndexFunc returns all indexes of item in the List, using the item function

func (List[T]) AllIndexed

func (l List[T]) AllIndexed(ok func(int, T) bool) bool

AllIndexed checks if all List item passes the ok function: (index, item)

func (List[T]) Any

func (l List[T]) Any(ok func(T) bool) bool

Any checks if List has an item that passes the ok function

func (List[T]) AnyIndexed

func (l List[T]) AnyIndexed(ok func(int, T) bool) bool

AnyIndexed checks if any List item passes the ok function: (index, item)

func (List[T]) Apply

func (l List[T]) Apply(task func(T) T) List[T]

Apply applies the task function to each item

func (List[T]) Cap

func (l List[T]) Cap() int

Cap returns the List capacity

func (List[T]) Copy

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

Copy creates a new List with copied items

func (List[T]) CountFunc

func (l List[T]) CountFunc(ok func(T) bool) int

CountFunc counts the number of item that passes the ok function

func (List[T]) Filter

func (l List[T]) Filter(keep func(T) bool) List[T]

Filter filters the List by only keeping items that pass the keep function

func (List[T]) FilterIndexed

func (l List[T]) FilterIndexed(keep func(int, T) bool) List[T]

FilterIndexed filters the List by only keeping items that pass the keep function: (index, item)

func (List[T]) Get

func (l List[T]) Get(index int) Option[T]

Get returns an Option with the List item if valid index, otherwise nil

func (List[T]) GetFuncOrDefault

func (l List[T]) GetFuncOrDefault(itemFn func(T) bool, defaultValue T) T

GetFuncOrDefault returns the first item that passes the item function, or returns the default value

func (List[T]) GetRandom

func (l List[T]) GetRandom() Option[T]

GetRandom gets a random item from List, and a flag which indicates if it is valid

func (List[T]) IndexFunc

func (l List[T]) IndexFunc(itemFn func(T) bool) int

IndexFunc returns the index of item (or -1 if not in List), using the item function

func (List[T]) IsEmpty

func (l List[T]) IsEmpty() bool

IsEmpty checks if List is empty

func (List[T]) Last

func (l List[T]) Last(rank int) Option[T]

Last returns the nth item from the back of the List (starts at 1), and a flag which indicates if it is valid

func (List[T]) LastIndex

func (l List[T]) LastIndex() int

LastIndex returns the List last index

func (List[T]) Len

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

Len returns the List length

func (List[T]) MapList

func (l List[T]) MapList(indexes []int) List[T]

MapList maps the indexes to List items. Can have zero values for invalid indexes

func (List[T]) MustGetRandom

func (l List[T]) MustGetRandom() T

MustGetRandom gets a random item from List, and panics if list is empty

func (List[T]) MustLast

func (l List[T]) MustLast(rank int) T

MustLast returns the nth item from the back of the List (starts at 1). Panics if rank is not 1 <= rank <= N, where N = length of List.

func (List[T]) NotAny

func (l List[T]) NotAny(ok func(T) bool) bool

NotAny checks if List has no item that passes the ok function

func (List[T]) NotAnyIndexed

func (l List[T]) NotAnyIndexed(ok func(int, T) bool) bool

NotAnyIndexed checks if no List item passes the ok function: (index, item)

func (List[T]) NotEmpty

func (l List[T]) NotEmpty() bool

NotEmpty checks if List is not empty

func (List[T]) Reduce

func (l List[T]) Reduce(initial T, reducer func(T, T) T) T

Reduce applies the reducer to each item to get the final result. The reducer function has the signature (result, item) => result

func (List[T]) RemoveAllFunc

func (l List[T]) RemoveAllFunc(itemFn func(T) bool) List[T]

RemoveAllFunc removes all items from List that passes the item function

func (List[T]) RemoveFunc

func (l List[T]) RemoveFunc(itemFn func(T) bool) (List[T], bool)

RemoveFunc removes the first item from List that passes the item function

func (List[T]) Shuffle

func (l List[T]) Shuffle()

Shuffle shuffles the List in place

func (List[T]) ToAny

func (l List[T]) ToAny() List[any]

ToAny creates a List of <any> items from the List

type Map

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

func ZipMap

func ZipMap[K comparable, V any](keys List[K], values List[V]) Map[K, V]

ZipMap creates a new Map by zipping the keys and values

func (Map[K, V]) Clear

func (m Map[K, V]) Clear()

Clear removes all Map entries

func (Map[K, V]) Copy

func (m Map[K, V]) Copy() Map[K, V]

Copy creates a new Map with copied entries

func (Map[K, V]) Delete

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

Delete removes key from the Map

func (Map[K, V]) Entries

func (m Map[K, V]) Entries() List[Tuple2[K, V]]

Entries returns the Map entries, in arbitrary order

func (Map[K, V]) Filter

func (m Map[K, V]) Filter(keep func(K, V) bool) Map[K, V]

Filter filters the Map, only keeping entries that pass the keep function

func (Map[K, V]) Get

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

Get returns an Option containing the value if key exists, otherwise nil

func (Map[K, V]) GetOrDefault

func (m Map[K, V]) GetOrDefault(key K, defaultValue V) V

GetOrDefault gets the value associated with key if it exists, otherwise returns the default value

func (Map[K, V]) HasKey

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

HasKey checks if Map has given key

func (Map[K, V]) HasKeyFunc

func (m Map[K, V]) HasKeyFunc(test func(K) bool) bool

HasKeyFunc checks if any Map key passes the test function

func (Map[K, V]) HasValueFunc

func (m Map[K, V]) HasValueFunc(test func(V) bool) bool

HasValueFunc checks if any Map value passes the test function

func (Map[K, V]) IsEmpty

func (m Map[K, V]) IsEmpty() bool

IsEmpty checks if Map is empty

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() List[K]

Keys returns the Map keys, in arbitrary order

func (Map[K, V]) KeysIter

func (m Map[K, V]) KeysIter() iter.Seq[K]

KeysIter returns an iterator for the Map keys

func (Map[K, V]) Len

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

Len returns the Map size

func (Map[K, V]) NoKey

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

NoKey checks if Map does not have given key

func (Map[K, V]) NoKeyFunc

func (m Map[K, V]) NoKeyFunc(test func(K) bool) bool

NoKeyFunc checks that no Map key passes the test function

func (Map[K, V]) NoValueFunc

func (m Map[K, V]) NoValueFunc(test func(V) bool) bool

NoValueFunc checks that no Map value passes the test function

func (Map[K, V]) NotEmpty

func (m Map[K, V]) NotEmpty() bool

NotEmpty checks if Map is not empty

func (Map[K, V]) SetDefault

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

SetDefault assigns default value to key, if key is not in Map

func (Map[K, V]) SortedEntriesFunc

func (m Map[K, V]) SortedEntriesFunc(sortFn func(Tuple2[K, V], Tuple2[K, V]) int) List[Tuple2[K, V]]

SortedEntriesFunc returns the Map entries in sorted order, using SortFn

func (Map[K, V]) SortedKeysFunc

func (m Map[K, V]) SortedKeysFunc(sortFn func(K, K) int) List[K]

SortedKeysFunc returns the Map keys in sorted order, using sortFn

func (Map[K, V]) SortedValuesFunc

func (m Map[K, V]) SortedValuesFunc(sortFn func(V, V) int) List[V]

SortedValuesFunc returns the Map values in sorted order, using SortFn

func (Map[K, V]) String

func (m Map[K, V]) String() string

String returns the string representation of Map, where keys are sorted

func (Map[K, V]) Unzip

func (m Map[K, V]) Unzip() (List[K], List[V])

Unzip returns the list of Map keys and values, where order of keys is same as corresponding values

func (Map[K, V]) Update

func (m Map[K, V]) Update(newMap Map[K, V])

Update adds the entries of new Map to current Map. If there are overlapping keys, new Map entries overwrite the old Map entries.

func (Map[K, V]) Values

func (m Map[K, V]) Values() List[V]

Values returns the Map values, in arbitrary order

func (Map[K, V]) ValuesIter

func (m Map[K, V]) ValuesIter() iter.Seq[V]

ValuesIter returns an iterator for the Map values

type NumList

type NumList[T number.Type] List[T]

func (NumList[T]) Product

func (l NumList[T]) Product() T

Product computes the product of number items

func (NumList[T]) Sum

func (l NumList[T]) Sum() T

Sum computes the sum of number items

func (NumList[T]) ToList

func (l NumList[T]) ToList() List[T]

ToList type coerces the NumList to List

type Option

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

func NewOption

func NewOption[T any](value *T) Option[T]

NewOption creates a new Option

func Nil

func Nil[T any]() Option[T]

Nil creates a new nil Option

func (Option[T]) Get

func (o Option[T]) Get() (T, bool)

Get returns the value and a Boolean indicating value is not nil

func (Option[T]) IsNil

func (o Option[T]) IsNil() bool

IsNil checks if Option is nil

func (Option[T]) NotNil

func (o Option[T]) NotNil() bool

NotNil checks if Option is not nil

func (Option[T]) String

func (o Option[T]) String() string

String returns the string representation of option

func (Option[T]) Value

func (o Option[T]) Value() T

Value returns the stored Option value, could be the zero value if Option is nil

type Pair

type Pair[T any] [2]T

Pair is a tuple with two values of the same type

func (Pair[T]) Unpack

func (p Pair[T]) Unpack() (T, T)

Unpack returns the unpacked Pair values

type Quad

type Quad[T any] [4]T

Quad is a tuple with four values of the same type

func (Quad[T]) Unpack

func (q Quad[T]) Unpack() (T, T, T, T)

Unpack returns the unpacked Quad values

type Queue

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

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue creates a new empty queue

func NewQueueFrom

func NewQueueFrom[T any](items []T) *Queue[T]

NewQueueFrom creates a new queue from list of items

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear removes all queue items

func (*Queue[T]) Copy

func (q *Queue[T]) Copy() *Queue[T]

Copy creates a new Queue with copied items

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() Option[T]

Dequeue returns an Option that contains the front item, and removes it from the queue. If the queue is empty, the Option contains nil

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue adds an item to the back of the queue

func (*Queue[T]) Front

func (q *Queue[T]) Front() Option[T]

Front returns an Option that contains the front item. If the queue is empty, the Option contains nil

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty checks if queue is empty

func (*Queue[T]) Items

func (q *Queue[T]) Items() List[T]

Items returns the List of queue items

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the number of items in the queue

func (*Queue[T]) MustDequeue

func (q *Queue[T]) MustDequeue() T

MustDequeue returns the front item and removes it from the queue, and panics if the queue is empty

func (*Queue[T]) MustFront

func (q *Queue[T]) MustFront() T

MustFront returns the front item, and panics if the queue is empty

func (*Queue[T]) NotEmpty

func (q *Queue[T]) NotEmpty() bool

NotEmpty if queue is not empty

func (*Queue[T]) String

func (q *Queue[T]) String() string

String returns the string representation of the queue

type Range

type Range[T number.Integer] struct {
	// contains filtered or unexported fields
}

Range represents a range of Integers from [start, end)

func NewInclusiveRange

func NewInclusiveRange[T number.Integer](first, last T) Range[T]

NewInclusiveRange creates a new Range from [first, last]

func NewRange

func NewRange[T number.Integer](start, end T) Range[T]

NewRange creates a new Range from [start, end)

func (Range[T]) Copy

func (r Range[T]) Copy() Range[T]

Copy creates a new Range copy

func (Range[T]) Has

func (r Range[T]) Has(item T) bool

Has checks if Integer is included in the Range

func (Range[T]) IsReversed

func (r Range[T]) IsReversed() bool

IsReversed checks if the Range is reversed

func (Range[T]) Len

func (r Range[T]) Len() int

Len returns the size of the Range

func (Range[T]) Limits

func (r Range[T]) Limits() (start, end T)

Limits returns the start, end limits of the Range

func (Range[T]) Product

func (r Range[T]) Product() T

Product computes the product of the Range

func (Range[T]) String

func (r Range[T]) String() string

String returns the string representation of the Range

func (Range[T]) Sum

func (r Range[T]) Sum() T

Sum computes the sum of the Range

func (Range[T]) ToList

func (r Range[T]) ToList() List[T]

ToList expands the range into a List of Integers

func (Range[T]) ToSlice

func (r Range[T]) ToSlice() []T

ToSlice expands the range into a slice of Integers

type Result

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

func Error

func Error[T any](err error) Result[T]

Error creates a new Result with error

func NewResult

func NewResult[T any](value T, err error) Result[T]

NewResult creates a new Result

func (Result[T]) Error

func (r Result[T]) Error() error

Error gets the Result error

func (Result[T]) Get

func (r Result[T]) Get() (T, bool)

Get returns the value and a Boolean indicating Result has no error

func (Result[T]) IsError

func (r Result[T]) IsError() bool

IsError checks if Result has an error

func (Result[T]) NotError

func (r Result[T]) NotError() bool

NotError checks if Result has no error

func (Result[T]) String

func (r Result[T]) String() string

String returns the string representation of Result

func (Result[T]) Value

func (r Result[T]) Value() T

Value returns the stored Result value, could be the zero value if Result is error

type Set

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

func NewSet

func NewSet[T comparable]() Set[T]

NewSet creates a new empty Set

func NewSetFrom

func NewSetFrom[T comparable](items []T) Set[T]

NewSetFrom creates a new Set from given items

func NewSetFunc

func NewSetFunc[T any, K comparable](items []T, keyFn func(T) K) Set[K]

NewSetFunc creates a new Set from given items, using the keyFn

func (Set[T]) Add

func (s Set[T]) Add(items ...T)

Add adds items to the Set

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all Set items

func (Set[T]) Copy

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

Copy creates a new Set with copied items

func (Set[T]) Delete

func (s Set[T]) Delete(item T)

Delete removes item from the Set

func (Set[T]) Difference

func (s Set[T]) Difference(s2 Set[T]) Set[T]

Difference computes the difference of two Sets

func (Set[T]) Has

func (s Set[T]) Has(item T) bool

Has checks if Set contains item

func (Set[T]) HasDifference

func (s Set[T]) HasDifference(s2 Set[T]) bool

HasDifference checks if two Sets have a non-empty difference

func (Set[T]) HasIntersection

func (s Set[T]) HasIntersection(s2 Set[T]) bool

HasIntersection checks if two Sets have a non-empty intersection

func (Set[T]) HasNo

func (s Set[T]) HasNo(item T) bool

HasNo checks if Set does not contain item

func (Set[T]) HasNoDifference

func (s Set[T]) HasNoDifference(s2 Set[T]) bool

HasNoDifference checks if two Sets have an empty difference

func (Set[T]) HasNoIntersection

func (s Set[T]) HasNoIntersection(s2 Set[T]) bool

HasNoIntersection checks if two Sets have an empty intersection

func (Set[T]) Intersection

func (s Set[T]) Intersection(s2 Set[T]) Set[T]

Intersection computes the intersection of two Sets

func (Set[T]) IsEmpty

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

IsEmpty checks if the Set is empty

func (Set[T]) Items

func (s Set[T]) Items() List[T]

Items returns the Set items, in arbitrary order

func (Set[T]) Len

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

Len returns the Set size

func (Set[T]) NotEmpty

func (s Set[T]) NotEmpty() bool

NotEmpty checks if the Set is not empty

func (Set[T]) String

func (s Set[T]) String() string

String returns the string representation of Set

func (Set[T]) Union

func (s Set[T]) Union(s2 Set[T]) Set[T]

Union computes the union of two Sets

type Stack

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

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates a new empty stack

func NewStackFrom

func NewStackFrom[T any](items []T) *Stack[T]

NewStackFrom creates a new stack from list of items (last item = stack top)

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Clear removes all stack items

func (*Stack[T]) Copy

func (s *Stack[T]) Copy() *Stack[T]

Copy creates a new Stack with copied items

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty checks if stack is empty

func (*Stack[T]) Items

func (s *Stack[T]) Items() List[T]

Items returns the List of stack items

func (*Stack[T]) Len

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

Len returns the number of items in the stack

func (*Stack[T]) MustPop

func (s *Stack[T]) MustPop() T

MustPop returns the top item and removes it from the stack, and panics if the stack is empty

func (*Stack[T]) MustTop

func (s *Stack[T]) MustTop() T

MustTop returns the top item, and panics if the stack is empty

func (*Stack[T]) NotEmpty

func (s *Stack[T]) NotEmpty() bool

NotEmpty checks if stack is not empty

func (*Stack[T]) Pop

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

Pop returns an Option that contains the top item and removes it from the stack. If the stack is empty, the Option contains nil

func (*Stack[T]) Push

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

Push adds an item to the top of the stack

func (*Stack[T]) String

func (s *Stack[T]) String() string

String returns the string representation of the stack

func (*Stack[T]) Top

func (s *Stack[T]) Top() Option[T]

Top returns an Option that contains the top item. If the stack is empty, the Option contains nil

type Triple

type Triple[T any] [3]T

Triple is a tuple with three values of the same type

func (Triple[T]) Unpack

func (t Triple[T]) Unpack() (T, T, T)

Unpack returns the unpacked Triple values

type Tuple2

type Tuple2[A, B any] struct {
	V1 A
	V2 B
}

Tuple2 is a tuple with two types

func NewTuple2

func NewTuple2[A, B any](v1 A, v2 B) Tuple2[A, B]

NewTuple2 creates a new Tuple2

func (Tuple2[A, B]) Unpack

func (t Tuple2[A, B]) Unpack() (A, B)

Unpack returns the unpacked Tuple2 values

type Tuple3

type Tuple3[A, B, C any] struct {
	V1 A
	V2 B
	V3 C
}

Tuple3 is a tuple with three types

func NewTuple3

func NewTuple3[A, B, C any](v1 A, v2 B, v3 C) Tuple3[A, B, C]

NewTuple3 creates a new Tuple3

func (Tuple3[A, B, C]) Unpack

func (t Tuple3[A, B, C]) Unpack() (A, B, C)

Unpack returns the unpacked Tuple3 values

type Tuple4

type Tuple4[A, B, C, D any] struct {
	V1 A
	V2 B
	V3 C
	V4 D
}

Tuple4 is a tuple with four types

func NewTuple4

func NewTuple4[A, B, C, D any](v1 A, v2 B, v3 C, v4 D) Tuple4[A, B, C, D]

NewTuple4 creates a new Tuple4

func (Tuple4[A, B, C, D]) Unpack

func (t Tuple4[A, B, C, D]) Unpack() (A, B, C, D)

Unpack returns the unpacked Tuple4 values

Jump to

Keyboard shortcuts

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