util

package
v0.1.2-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Integer](n T) T

func AppendUnique

func AppendUnique[T comparable](slice []T, elems ...T) []T

func AssertMustError

func AssertMustError(err error, target ...error)

func AssertNoError

func AssertNoError(err error, prefix ...string)

func AssertNotNil

func AssertNotNil[T comparable](el T)

func Assertf

func Assertf(cond bool, format string, args ...any)

Assertf with optionally deferred evaluation of arguments

func CallWithTimeout

func CallWithTimeout(ctx context.Context, timeout time.Duration, fun, onTimeout func())

CallWithTimeout calls fun. If it does not finish in timeout period, calls onTimeout

func CatchPanicOrError

func CatchPanicOrError(f func() error, includeStack ...bool) error

func Cond

func Cond[T any](cond bool, ifTrue, ifFalse T) T

func DoUntil

func DoUntil(bodyFun func(), cond func() bool)

func ED25519PrivateKeyFromHexString

func ED25519PrivateKeyFromHexString(str string) (ed25519.PrivateKey, error)

func EqualSlices

func EqualSlices[T comparable](s1, s2 []T) bool

func ErrorConditionf

func ErrorConditionf(cond bool, format string, args ...any) error

func EvalLazyArgs

func EvalLazyArgs(args ...any) []any

func ForEachUniquePair

func ForEachUniquePair[T any](sl []T, fun func(a1, a2 T) bool)

func IndexOfMaximum

func IndexOfMaximum[T any](lst []T, less func(i1, i2 int) bool) int

IndexOfMaximum slice cannot be empty

func IsNil

func IsNil(p interface{}) bool

func KeysFiltered

func KeysFiltered[K comparable, V any](m map[K]V, cond func(k K) bool) []K

func KeysFilteredByValues

func KeysFilteredByValues[K comparable, V any](m map[K]V, cond func(k K, v V) bool) []K

func KeysSorted

func KeysSorted[K comparable, V any](m map[K]V, less func(k1, k2 K) bool) []K

func List

func List[T any](elems ...T) []T

func MakeErrFuncForPrefix

func MakeErrFuncForPrefix(prefix string) func(err interface{}, args ...interface{}) error

func MakeRange

func MakeRange[T constraints.Integer](from, toIncl T) []T

func Maximum

func Maximum[T any](lst []T, less func(el1, el2 T) bool) T

func Median

func Median[T Number](data []T) T

func MustLastElement

func MustLastElement[T any](sl []T) T

func MustPrivateKeyFromHexString

func MustPrivateKeyFromHexString(k string) ed25519.PrivateKey

func Panicf

func Panicf(format string, args ...any)

func Percent

func Percent(n, d int) float32

func PercentString

func PercentString(n, d int) string

func PrivateKeyFromHexString

func PrivateKeyFromHexString(k string) (ed25519.PrivateKey, error)

func PurgeFilesInDirectory

func PurgeFilesInDirectory(directory, namePattern string, keepLatest int) error

func PurgeSlice

func PurgeSlice[T any](slice []T, filter func(el T) bool) []T

PurgeSlice filters elements on the same underlying array Element remains in the array only if 'filter' function returns true The elements of the array which are not covered by slice are nullified. This may be important when slice contains pointers, which, in turn, may lead to hidden memory leak because GC can't remove them

func Quartiles

func Quartiles[T Number](data []T) (ret [3]T)

Quartiles returns quartiles 1, 2, 3

func RandomElements

func RandomElements[T any](n int, elems ...T) []T

RandomElements selects n random elements from a slice, which has more elements than n

func RandomNOutOfMPractical

func RandomNOutOfMPractical(n, m int) []int

RandomNOutOfMPractical tries to generate n different random numbers from [0:m]. Only makes 2 * n attempts, so result may be less than n numbers

func Ref

func Ref[T any](v T) *T

func RequireErrorWith

func RequireErrorWith(t *testing.T, err error, fragments ...string)

func RequirePanicOrErrorWith

func RequirePanicOrErrorWith(t *testing.T, f func() error, fragments ...string)

func RunWrappedRoutine

func RunWrappedRoutine(name string, fun func(), onPanic func(err error) bool)

func StringsLess

func StringsLess(s1, s2 string) bool

func Th

func Th[T Integer](v T, separator ...string) string

Th makes string representation of the integer with thousands separator

func TrimSlice

func TrimSlice[T any](slice []T, maxLen int) []T

TrimSlice trims slice on the same underlying array. Nullifies trimmed elements

func ValuesFiltered

func ValuesFiltered[K comparable, V any](m map[K]V, filter func(v V) bool) []V

Types

type Integer

type Integer interface {
	int | uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64
}

type Number

type Number interface {
	constraints.Float | constraints.Integer | time.Duration
}

Directories

Path Synopsis
Package lazybytes is a way to treat byte data as serialized arrays or, recursively, as trees of byte slices It is used for fast, safe and uniform serialization/deserialization in 'lazy' way: the bytes are only deserialized when there's a need to access element of the array or a tree The read-only lazy array and tree are thread safe.
Package lazybytes is a way to treat byte data as serialized arrays or, recursively, as trees of byte slices It is used for fast, safe and uniform serialization/deserialization in 'lazy' way: the bytes are only deserialized when there's a need to access element of the array or a tree The read-only lazy array and tree are thread safe.
Package sema implements semaphore with timeout
Package sema implements semaphore with timeout

Jump to

Keyboard shortcuts

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