generics

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MPL-2.0 Imports: 2 Imported by: 24

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToSliceOfAny

func ConvertToSliceOfAny[T any](ts []T) (ret []any)

I take it there's no way to do this with a generic return slice element type.

func InitNew

func InitNew[T any](p **T)

func MakeChanWithLen

func MakeChanWithLen[T chan U, U any, L constraints.Integer](m *T, l L)

I can't seem to make a common function for things the make function works with. "no core type"

func MakeMap

func MakeMap[K comparable, V any, M ~map[K]V](pm *M)

Does this exist in the maps package?

func MakeMapIfNil

func MakeMapIfNil[K comparable, V any, M ~map[K]V](pm *M)

func MakeMapIfNilAndSet deprecated

func MakeMapIfNilAndSet[K comparable, V any](pm *map[K]V, k K, v V) (added bool)

Deprecated: Use MakeMapIfNil and MapInsert separately.

func MakeMapWithCap

func MakeMapWithCap[K comparable, V any, M ~map[K]V, C constraints.Integer](pm *M, cap C)

func MakeSliceWithCap

func MakeSliceWithCap[T any, L constraints.Integer](slice *[]T, cap L)

func MakeSliceWithLength

func MakeSliceWithLength[T any, L constraints.Integer](slice *[]T, length L)

func MapContains

func MapContains[K comparable, V any, M ~map[K]V](m M, k K) bool

func MapMustGet

func MapMustGet[K comparable, V any, M ~map[K]V](m M, k K) V

func Max

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

func Min

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

func PtrTo

func PtrTo[T any](t T) *T

func Range

func Range[T constraints.Integer](n T) []struct{}

Returns a zero-size, zero-allocation slice of the given length that can be used with range to loop n times. Also has the advantage of not requiring a loop variable. Similar to bradfitz's iter.N, and my clone in anacrolix/missinggo.

func ReverseSlice

func ReverseSlice[T any](slice []T)

func Reversed

func Reversed[T any](slice []T) []T

func SetZero

func SetZero[T any](p *T)

func Singleton

func Singleton[T any](t T) []T

func SliceDrop

func SliceDrop[T any](n int, slice []T) []T

func SliceMap

func SliceMap[From, To any](froms []From, convert func(From) To) []To

Surely you should just pass iterator functions around instead. Go sux.

func SlicePop

func SlicePop[T any](slice *[]T) T

Pops the last element from the slice and returns it. Panics if the slice is empty, or if the slice is nil.

func SliceTake

func SliceTake[T any](n int, slice []T) []T

func UnwrapErrorTuple

func UnwrapErrorTuple[T any](t T, err error) T

func ZeroValue

func ZeroValue[T any]() (zero T)

Types

type Future

type Future[T any] interface {
	Wait(ctx context.Context) T
}

type Option

type Option[V any] struct {
	// Value must be zeroed when Ok is false for deterministic comparability.
	Value V
	// bool is the smallest type, so putting it at the end increases the chance it can be packed
	// with Value.
	Ok bool
}

func MapInsert

func MapInsert[K comparable, V any, M ~map[K]V](m M, k K, v V) Option[V]

func None

func None[V any]() Option[V]

func OptionFromTuple

func OptionFromTuple[T any](t T, ok bool) Option[T]

func SliceGet

func SliceGet[T any, I constraints.Integer](slice []T, index I) (ret Option[T])

func Some

func Some[V any](value V) Option[V]

func (Option[V]) AndThen deprecated

func (me Option[V]) AndThen(f func(V) Option[V]) Option[V]

Deprecated: Use option.AndThen

func (*Option[V]) Set

func (me *Option[V]) Set(v V)

func (*Option[V]) SetFromTuple

func (me *Option[V]) SetFromTuple(v V, ok bool)

func (*Option[V]) SetNone

func (me *Option[V]) SetNone()

func (*Option[V]) SetSomeZeroValue

func (me *Option[V]) SetSomeZeroValue()

func (Option[V]) Unwrap

func (me Option[V]) Unwrap() V

func (Option[V]) UnwrapOr

func (me Option[V]) UnwrapOr(or V) V

func (Option[V]) UnwrapOrZeroValue

func (me Option[V]) UnwrapOrZeroValue() (_ V)

func (*Option[V]) UnwrapPtr

func (me *Option[V]) UnwrapPtr() *V

type Result

type Result[T any] struct {
	Ok  T
	Err error
}

func ResultFromTuple

func ResultFromTuple[T any](t T, err error) Result[T]

func (Result[T]) AsTuple

func (r Result[T]) AsTuple() (T, error)

func (Result[T]) ToOption

func (r Result[T]) ToOption() Option[T]

func (Result[T]) Unwrap

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

Directories

Path Synopsis
Package heap provides heap operations for any type that implements heap.Interface.
Package heap provides heap operations for any type that implements heap.Interface.
Package list implements a doubly linked list.
Package list implements a doubly linked list.

Jump to

Keyboard shortcuts

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