tl

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

tl

Golang generic utilities (Template Library)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Advance

func Advance[T any](iter Iter[T], count int)

Advance advances iter a number of `count` positions.

func AntiJoin

func AntiJoin[T comparable](a, b []T) []T

AntiJoin returns a slice containing the elements that are present in `a` but not in `b`.

Example
package main

import (
	"fmt"

	"github.com/dgrr/tl"
)

func main() {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	b := []int{5, 7, 9, 11, 13}

	antijoined := tl.AntiJoin(a, b)

	fmt.Println("All", antijoined)
}
Output:

func AntiJoinFn

func AntiJoinFn[T any](a, b []T, cmpFn func(a, b T) bool) (r []T)

AntiJoinFn performs an AntiJoin operation for non-comparable types.

See AntiJoin for more.

func Avg

func Avg[T constraints.Integer | constraints.Float](a ...T) (r T)

func Contains

func Contains[T comparable](vs []T, e T) bool

Contains returns where `e` is present in `vs`.

func ContainsFn

func ContainsFn[T any](vs []T, cmpFn CompareFunc[T]) bool

ContainsFn returns where `vs` contains an element using `cmpFn`.

func Delete

func Delete[T comparable](set []T, value T) []T

Delete removes an element from a set. Notice that this function only removes the first occurrence.

func DeleteAll added in v0.2.1

func DeleteAll[T comparable](set []T, value T) []T

DeleteAll deletes all occurrences of a matching value.

func Filter

func Filter[T any](set []T, cmpFn CompareFunc[T]) []T

Filter filters a set of values using `cmpFn`.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/dgrr/tl"
)

func main() {
	filterThis := []string{
		"Each type parameter has a type",
		"constraint that acts as a kind of",
		"meta-type for the type parameter",
		"Each type constraint specifies the permissible",
		"type arguments that calling",
		"code can use for the respective",
		"type parameter",
	}

	// filter out the strings containing `type`.
	afterFilter := tl.Filter(filterThis, func(x string) bool {
		return !strings.Contains(x, "type")
	})

	fmt.Println(strings.Join(filterThis, "\n"))
	fmt.Println("--- after ----")
	fmt.Println(strings.Join(afterFilter, "\n"))
}
Output:

func FilterInPlace

func FilterInPlace[T any](set []T, cmpFn CompareFunc[T]) []T

FilterInPlace filters the set in-place, meaning that it will not create a new slice like in Filter.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/dgrr/tl"
)

func main() {
	filterThis := []string{
		"Each type parameter has a type",
		"constraint that acts as a kind of",
		"meta-type for the type parameter",
		"Each type constraint specifies the permissible",
		"type arguments that calling",
		"code can use for the respective",
		"type parameter",
	}

	// filter out the strings containing `type`.
	filterThis = tl.FilterInPlace(filterThis, func(x string) bool {
		return !strings.Contains(x, "type")
	})

	fmt.Println(strings.Join(filterThis, "\n"))
}
Output:

func Join

func Join[T comparable](a, b []T) []T

Join returns a slice containing the elements that are present in `a` and `b`.

Example
package main

import (
	"fmt"

	"github.com/dgrr/tl"
)

func main() {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	b := []int{5, 7, 9, 11, 13}

	joined := tl.Join(a, b)

	fmt.Println("All", joined)
}
Output:

func JoinFn

func JoinFn[T any](a, b []T, cmpFn func(a, b T) bool) (r []T)

JoinFn performs an Join operation for non-comparable types.

See Join for more.

func Map

func Map[T, E any](set []T, fn func(T) E) []E

Map maps the values of `set` using `fn`.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/dgrr/tl"
)

func main() {
	numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	numbersToString := tl.Map(numbers, func(number int) string {
		return strconv.Itoa(number)
	})

	fmt.Println(numbers)
	fmt.Println(numbersToString)
}
Output:

func Max

func Max[T constraints.Ordered](numbers ...T) (r T)

Max returns the maximum instance of a set of numbers.

func Merge

func Merge[T comparable](a []T, more ...[]T) (r []T)

Merge merges multiple slices into one avoiding duplicates.

Returns a slice containing all the elements in a, and more... without duplicates.

Example
package main

import (
	"fmt"

	"github.com/dgrr/tl"
)

func main() {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	b := []int{5, 7, 9, 11, 13}

	merged := tl.Merge(a, b)

	fmt.Println("All", merged)
}
Output:

func MergeFn

func MergeFn[T any](cmpFn func(a, b T) bool, a []T, more ...[]T) (r []T)

MergeFn performs a Merge operation for non-comparable types.

See Merge for more.

func Min

func Min[T constraints.Ordered](a, b T) (r T)

func SearchFn

func SearchFn[T any](vs []T, cmpFn CompareFunc[T]) int

SearchFn iterates over `vs` comparing the values using `cmpFn`.

Returns the index to the element if found, -1 otherwise.

func Sum

func Sum[T constraints.Integer | constraints.Float](a ...T) (r T)

Types

type Bytes

type Bytes Vec[byte]

Bytes wraps the behavior of Vec as Vec[byte] adding new methods on top.

func BytesFrom

func BytesFrom(bts []byte) Bytes

BytesFrom creates a Bytes instance from `bts` bytes.

func NewBytes

func NewBytes(size, capacity int) Bytes

NewBytes returns a Bytes instance with size `size` and capacity `capacity`.

func (*Bytes) Append

func (b *Bytes) Append(others ...byte)

Append appends bytes at the end of Bytes.

func (Bytes) Cap

func (b Bytes) Cap() int

Cap returns the capacity of `b`.

This function is equivalent to `cap(b))`.

func (*Bytes) CopyFrom

func (b *Bytes) CopyFrom(other Bytes)

CopyFrom copies the bytes from `other` to `b`.

func (Bytes) Index

func (b Bytes) Index(other Bytes) int

Index returns the index of `other` in `b`.

This function is equivalent to bytes.Index(b, other).

func (Bytes) IndexByte added in v0.2.1

func (b Bytes) IndexByte(c byte) int

IndexByte returns the index of `c` in `b`.

This function is equivalent to bytes.IndexByte(b, c).

func (Bytes) Len

func (b Bytes) Len() int

Len returns the length of `b`.

This function is equivalent to `len(b))`.

func (*Bytes) LimitReadFrom

func (b *Bytes) LimitReadFrom(r io.Reader, n int) (int64, error)

LimitReadFrom is like ReadFrom but it is limited to `n` bytes.

func (Bytes) LimitWriteTo

func (b Bytes) LimitWriteTo(w io.Writer, n int) (int64, error)

LimitWriteTo writes a limited amount of `n` bytes to an io.Writer.

func (*Bytes) Push

func (b *Bytes) Push(bts ...byte)

Push pushes the `bts` to the beginning of Bytes.

func (Bytes) ReadFrom

func (b Bytes) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads bytes from an io.Reader into `b`.

This function does NOT append bytes, it uses the existing buffer.

Implements the io.ReaderFrom interface.

func (*Bytes) Reserve

func (b *Bytes) Reserve(n int)

Reserve increases the size of Bytes if needed.

The call doesn't produce any allocation if `n` < cap(b).

func (*Bytes) Resize

func (b *Bytes) Resize(n int)

Resize increases or decreases the size of Bytes.

func (Bytes) Slice

func (b Bytes) Slice(low, max int) Bytes

Slice returns a sliced Bytes using indexes starting from `low` and ending in `max`.

func (Bytes) String

func (b Bytes) String() string

String converts `b` to a string.

This function produces an allocation. To avoid the allocation use UnsafeString.

func (Bytes) UnsafeString

func (b Bytes) UnsafeString() string

UnsafeString converts `b` to a string without producing allocations.

func (Bytes) WriteTo

func (b Bytes) WriteTo(w io.Writer) (int64, error)

WriteTo writes the bytes to an io.Writer.

Implements the io.WriterTo interface.

type CompareFunc

type CompareFunc[T any] func(T) bool

type Iter

type Iter[T any] interface {
	// Next is called to iterate to the next element.
	//
	// Returns true if a next element is available, false otherwise.
	Next() bool
	// Get returns the current element.
	Get() T
	// GetPtr returns a pointer to the current element.
	GetPtr() *T
}

Iter defines an iterator interface.

type IterBidir

type IterBidir[T any] interface {
	Iter[T]
	// Back is like next but for going backwards. It moves the iterator to the previous position.
	// For some implementations that might mean going in reverse mode (not going backwards).
	Back() bool
}

IterBidir defines a bidirectional iterator.

type IterDrop

type IterDrop[T any] interface {
	Iter[T]
	// Drop removes the current element from the iterator.
	Drop()
}

IterDrop implements an iterator which current element can be dropped.

type IterDropBidir

type IterDropBidir[T any] interface {
	IterDrop[T]
	IterBidir[T]
}

IterDropBidir merges IterBidir and IterDrop in one interface.

type List

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

List defines a doubly linked list.

func (*List[T]) Back

func (list *List[T]) Back() (opt OptionalPtr[T])

Back returns an optional to the last element of the queue.

func (*List[T]) Front

func (list *List[T]) Front() (opt OptionalPtr[T])

Front returns an optional to the first element of the queue.

func (*List[T]) Iter

func (list *List[T]) Iter() IterDropBidir[T]

Iter returns an iterator for the List.

func (*List[T]) PopBack

func (list *List[T]) PopBack() (opt OptionalPtr[T])

PopBack pops the last element if any.

func (*List[T]) PopFront

func (list *List[T]) PopFront() (opt OptionalPtr[T])

PopFront pops the first element if any.

func (*List[T]) PushBack

func (list *List[T]) PushBack(v T) *ListElement[T]

PushBack appends an element to the back of the queue.

func (*List[T]) PushFront

func (list *List[T]) PushFront(v T) *ListElement[T]

PushFront pushes an element to the front of the queue.

func (*List[T]) Reset

func (list *List[T]) Reset()

Reset resets the list.

func (*List[T]) Size

func (list *List[T]) Size() int

Size returns the size of the linked list (number of elements inside the list).

type ListElement

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

ListElement is an element in a List.

func (*ListElement[T]) Drop

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

Drop drops the current element from the list.

func (*ListElement[T]) Get

func (e *ListElement[T]) Get() T

Get returns the value of the element.

func (*ListElement[T]) GetPtr

func (e *ListElement[T]) GetPtr() *T

GetPtr returns a pointer to the value of the element.

type Optional

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

Optional defines a value that can be optional. So, if might be defined or not.

func MakeOptional

func MakeOptional[T any](v T) (opt Optional[T])

MakeOptional returns an Optional with a value `v`.

func NewOptional

func NewOptional[T any](v *T) (opt Optional[T])

NewOptional returns an Optional with a value `v` if `v` is not nil.

func None

func None[T any]() (opt Optional[T])

None returns an empty Optional.

func (Optional[T]) From

func (opt Optional[T]) From(v T) Optional[T]

From sets the value `v` to the Optional.

func (Optional[T]) Get

func (opt Optional[T]) Get() T

Get returns the value. The function doesn't check whether the value is set or not.

func (Optional[T]) HasValue

func (opt Optional[T]) HasValue() bool

HasValue returns true if Optional contains a valid value.

func (Optional[T]) Or

func (opt Optional[T]) Or(v T) Optional[T]

Or sets the value `v` to the Optional only if no value is already present.

func (Optional[T]) Ptr

func (opt Optional[T]) Ptr() *T

Ptr returns a pointer to the value or nil if there's no value.

func (*Optional[T]) Reset

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

Reset sets the Optional as invalid.

func (*Optional[T]) Set

func (opt *Optional[T]) Set(v T)

Set sets a value `v` to the Optional.

type OptionalPtr

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

OptionalPtr is like Optional but instead of holding a copy of the value, it holds a pointer.

func MakeOptionalPtr

func MakeOptionalPtr[T any](v *T) (opt OptionalPtr[T])

MakeOptionalPtr returns an OptionalPtr using the value `v`.

func (OptionalPtr[T]) Get

func (opt OptionalPtr[T]) Get() *T

Get returns a pointer to the value, because the value is a pointer already.

func (*OptionalPtr[T]) GetValue

func (opt *OptionalPtr[T]) GetValue() T

GetValue returns the value inside the Optional's pointer. This function might panic if the value is not defined (aka value == nil).

func (OptionalPtr[T]) HasValue

func (opt OptionalPtr[T]) HasValue() bool

HasValue returns true if a value is present.

func (OptionalPtr[T]) Or

func (opt OptionalPtr[T]) Or(v *T) OptionalPtr[T]

Or sets the value `v` to the Optional only if not value is already present.

func (OptionalPtr[T]) Ptr

func (opt OptionalPtr[T]) Ptr() *T

Ptr returns a pointer to the value.

func (*OptionalPtr[T]) Reset

func (opt *OptionalPtr[T]) Reset()

Reset resets the OptionalPtr's value.

func (*OptionalPtr[T]) Set

func (opt *OptionalPtr[T]) Set(v *T)

Set sets the value `v` to the OptionalPtr.

type Pair

type Pair[T, U any] struct {
	// contains filtered or unexported fields
}

Pair defines a pair of values.

func MakePair

func MakePair[T, U any](t T, u U) Pair[T, U]

MakePair returns a Pair instance using (t, u).

func (Pair[T, U]) Both

func (p Pair[T, U]) Both() (T, U)

Both returns both values at the same time.

func (Pair[T, U]) First

func (p Pair[T, U]) First() T

First returns the first value.

func (Pair[T, U]) Second

func (p Pair[T, U]) Second() U

Second returns the second value.

func (Pair[T, U]) Swap

func (p Pair[T, U]) Swap() Pair[U, T]

Swap returns a new Pair swapping the place of the values.

type Queue

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

Queue defines a queue data structure.

func (*Queue[T]) Front

func (q *Queue[T]) Front() (v Optional[T])

Front returns the first element if any.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (v Optional[T])

Pop pops from the beginning of the queue.

func (*Queue[T]) PushBack

func (q *Queue[T]) PushBack(data T)

PushBack pushes to the back of the queue.

func (*Queue[T]) PushFront

func (q *Queue[T]) PushFront(data T)

PushFront pushes `data` to the beginning of the queue.

func (*Queue[T]) Reset

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

Reset resets the queue.

type Ring

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

Ring is a Multiple-Producer Multiple-Consumer (MPMC) circular buffer data structure.

func NewRing

func NewRing[T any](size int) *Ring[T]

NewRing returns a ring of type T with the size rounded up to the nearest power of 2.

func (*Ring[T]) Pop

func (ring *Ring[T]) Pop() (value T, ok bool)

Pop takes a value from the ring. Returns the (value, true) or (value, false)

func (*Ring[T]) Push

func (ring *Ring[T]) Push(value T) bool

Push adds a value to the ring.

If the ring is full Push returns false. If the task has been pushed it will return true.

type Tree

type Tree[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

func (*Tree[Key, Value]) Data

func (tree *Tree[Key, Value]) Data() Optional[Value]

func (*Tree[Key, Value]) Del

func (tree *Tree[Key, Value]) Del(path ...Key)

func (*Tree[Key, Value]) Depth

func (tree *Tree[Key, Value]) Depth() int

func (*Tree[Key, Value]) Fetch

func (tree *Tree[Key, Value]) Fetch(path ...Key) (opt Optional[Value])

func (*Tree[Key, Value]) Get

func (tree *Tree[Key, Value]) Get(path ...Key) (depth int, opt Optional[Value])

func (*Tree[Key, Value]) GetTree

func (tree *Tree[Key, Value]) GetTree(path ...Key) *Tree[Key, Value]

func (*Tree[Key, Value]) Name

func (tree *Tree[Key, Value]) Name() Key

func (*Tree[Key, Value]) Path

func (tree *Tree[Key, Value]) Path() []Key

func (*Tree[Key, Value]) Range

func (tree *Tree[Key, Value]) Range(fn func(*Tree[Key, Value]) bool, path ...Key)

func (*Tree[Key, Value]) RangeAll

func (tree *Tree[Key, Value]) RangeAll(fn func(*Tree[Key, Value]) bool)

func (*Tree[Key, Value]) RangeLevel

func (tree *Tree[Key, Value]) RangeLevel(fn func(*Tree[Key, Value]) bool, level int)

func (*Tree[Key, Value]) RangeLimit

func (tree *Tree[Key, Value]) RangeLimit(fn func(*Tree[Key, Value]) bool, maxDepth int)

func (*Tree[Key, Value]) Set

func (tree *Tree[Key, Value]) Set(data Value, path ...Key)

func (*Tree[Key, Value]) SetRange

func (tree *Tree[Key, Value]) SetRange(data Value, lvl int)

func (*Tree[Key, Value]) Trees

func (tree *Tree[Key, Value]) Trees() []*Tree[Key, Value]

type Vec

type Vec[T any] []T

Vec defines a slice of type T.

func MakeVec

func MakeVec[T any](elmnts ...T) Vec[T]

MakeVec returns a vector of type T with the elements `elmnts`.

func MakeVecSize

func MakeVecSize[T any](size, capacity int) Vec[T]

MakeVecSize returns a vector with size `size` and capacity `capacity`.

func (*Vec[T]) Append

func (vc *Vec[T]) Append(elmnts ...T)

func (Vec[T]) Back

func (vc Vec[T]) Back() (opt OptionalPtr[T])

func (Vec[T]) Cap

func (vc Vec[T]) Cap() int

func (Vec[T]) Contains

func (vc Vec[T]) Contains(cmpFn CompareFunc[T]) bool

func (*Vec[T]) DelByIndex

func (vc *Vec[T]) DelByIndex(i int) (val T, erased bool)

func (*Vec[T]) Filter

func (vc *Vec[T]) Filter(cmpFn CompareFunc[T]) (val T, erased bool)

func (Vec[T]) Front

func (vc Vec[T]) Front() (opt OptionalPtr[T])

func (Vec[T]) Get

func (vc Vec[T]) Get(i int) T

func (Vec[T]) Index

func (vc Vec[T]) Index(cmpFn CompareFunc[T]) int

func (Vec[T]) Len

func (vc Vec[T]) Len() int

func (*Vec[T]) PopBack

func (vc *Vec[T]) PopBack() (opt OptionalPtr[T])

func (*Vec[T]) PopFront

func (vc *Vec[T]) PopFront() (opt OptionalPtr[T])

func (*Vec[T]) Push

func (vc *Vec[T]) Push(elmnts ...T)

func (*Vec[T]) Reserve

func (vc *Vec[T]) Reserve(n int)

func (*Vec[T]) Resize

func (vc *Vec[T]) Resize(n int)

func (Vec[T]) Search

func (vc Vec[T]) Search(cmpFn CompareFunc[T]) (v T, ok bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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