gen

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 0 Imported by: 0

README

package gen has some generic helpers inside

Documentation

Overview

Package constraints defines a set of useful constraints to be used with type parameters.

Package gen contains a bunch of generic functions that will probably be in the Go std lib someday

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Integer | Float](a T) T

func Clamp

func Clamp[T Ordered](v, min, max T) T

func CopyMap

func CopyMap[K comparable, V any](src map[K]V) map[K]V

Return a copy of the map

func CopySlice

func CopySlice[T any](src []T) []T

Return a copy of the slice

func DeleteFirst

func DeleteFirst[T comparable](slice []T, elem T) []T

Finds an element in the slice and returns a copy with that element removed. If the element does not exist, returns the original slice. If the element exists more than once, removes only the first one.

func DeleteFromSliceOrdered

func DeleteFromSliceOrdered[T any](src []T, i int) []T

Deletes the i'th element of the slice, and returns a new slice. Preserves order, but is much slower than DeleteFromSliceUnordered.

func DeleteFromSliceUnordered

func DeleteFromSliceUnordered[T any](src []T, i int) []T

Deletes the i'th element of the slice, and returns a new slice. Does not preserve order, but is much faster than DeleteFromSliceOrdered.

func DrainChannel

func DrainChannel[T any](ch chan T)

Drain the channel and discard the contents

func DrainChannelIntoSlice

func DrainChannelIntoSlice[T any](ch chan T) []T

DrainChannelIntoSlice reads from a channel until it is empty, and returns all items in a slice

func Filter

func Filter[T any](items []T, predicate func(T) bool) []T

Filter takes a slice of any type and a predicate function, returning a new slice with only the elements that satisfy the predicate.

func IndexOf

func IndexOf[T comparable](src []T, v T) int

Returns the index of the first 'v' in 'src', or -1 if not found

func IsChannelClosed

func IsChannelClosed[T any](ch chan T) bool

func Max

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

func Min

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

func Mode

func Mode[T comparable](src []T) (mode T, count int)

func WaitForChannelToClose

func WaitForChannelToClose[T any](ch chan T)

Types

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type. If future releases of Go add new predeclared complex numeric types, this constraint will be modified to include them.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.

type Ordered

type Ordered interface {
	Integer | Float | ~string
}

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is a constraint that permits any signed integer type. If future releases of Go add new predeclared signed integer types, this constraint will be modified to include them.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned is a constraint that permits any unsigned integer type. If future releases of Go add new predeclared unsigned integer types, this constraint will be modified to include them.

Jump to

Keyboard shortcuts

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