Documentation ¶
Overview ¶
Package funcs is useful Go functions
SPDX-License-Identifier: Apache-2.0
Index ¶
- Constants
- func And[T any](filters ...func(T) bool) func(T) bool
- func AssertType[T any](msg string, v any) (T, error)
- func CamelCaseToSnakeCase(camel string) string
- func Compose[T any](f0 func(T) T, fns ...func(T) T) func(T) T
- func Compose10[P, Q, R, S, T, U, V, W, X, Y, Z any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ...) func(P) Z
- func Compose2[P, Q, R any](f0 func(P) Q, f1 func(Q) R) func(P) R
- func Compose3[P, Q, R, S any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S) func(P) S
- func Compose4[P, Q, R, S, T any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T) func(P) T
- func Compose5[P, Q, R, S, T, U any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U) func(P) U
- func Compose6[P, Q, R, S, T, U, V any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ...) func(P) V
- func Compose7[P, Q, R, S, T, U, V, W any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ...) func(P) W
- func Compose8[P, Q, R, S, T, U, V, W, X any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ...) func(P) X
- func Compose9[P, Q, R, S, T, U, V, W, X, Y any](f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ...) func(P) Y
- func ConvertToMap[K comparable, V any](msg string, v any) (res map[K]V, err error)
- func ConvertToSlice[T any](msg string, v any) (res []T, err error)
- func ConvertToSlice2[T any](msg string, v any) (res [][]T, err error)
- func Equal[T comparable](val T) func(T) bool
- func FirstValue2[T, U any](t T, u U) T
- func FirstValue3[T, U, V any](t T, u U, v V) T
- func GreaterThan[T constraint.Ordered](val T) func(T) bool
- func GreaterThanEqual[T constraint.Ordered](val T) func(T) bool
- func IgnoreResult[T any](fn func() T) func()
- func In[T comparable](val ...T) func(T) bool
- func IsNegative[T constraint.Signed]() func(T) bool
- func IsNil[T any]() func(T) bool
- func IsNilValue(val any) bool
- func IsNonNegative[T constraint.Signed]() func(T) bool
- func IsNonNil[T any]() func(T) bool
- func IsPositive[T constraint.Signed]() func(T) bool
- func LessThan[T constraint.Ordered](val T) func(T) bool
- func LessThanEqual[T constraint.Ordered](val T) func(T) bool
- func Map2Set[K1, K2 comparable, V any](mp *map[K1]map[K2]V, key1 K1, key2 K2, value V)
- func Map2SliceAdd[K1, K2 comparable, V any](mp *map[K1]map[K2][]V, key1 K1, key2 K2, value V)
- func Map2SliceRemove[K1, K2, V comparable](mp map[K1]map[K2][]V, key1 K1, key2 K2, value V, all ...bool)
- func Map2SliceRemoveUncomparable[K1, K2 comparable, V any](mp map[K1]map[K2][]V, key1 K1, key2 K2, value V, all ...bool)
- func Map2Test[K1, K2 comparable, V any](mp map[K1]map[K2]V, key1 K1, key2 K2) (exists bool)
- func Map2Unset[K1, K2 comparable, V any](mp map[K1]map[K2]V, key1 K1, key2 K2)
- func MapIndex[K comparable, V any](mp map[K]V, key K, defawlt ...V) V
- func MapKeysToSlice[K comparable, V any](mp map[K]V) []K
- func MapSet[K comparable, V any](mp *map[K]V, key K, value V)
- func MapSliceAdd[K comparable, V any](mp *map[K][]V, key K, value V)
- func MapSliceRemove[K, V comparable](mp map[K][]V, key K, value V, all ...bool)
- func MapSliceRemoveUncomparable[K comparable, V any](mp map[K][]V, key K, value V, all ...bool)
- func MapSortBy[K comparable, V any](mp map[K]V, less func(K, K) bool) []tuple.Two[K, V]
- func MapSortCmp[K constraint.Cmp[K], V any](mp map[K]V) []tuple.Two[K, V]
- func MapSortComplex[K constraint.Complex, V any](mp map[K]V) []tuple.Two[K, V]
- func MapSortOrdered[K constraint.Ordered, V any](mp map[K]V) []tuple.Two[K, V]
- func MapTest[K comparable, V any](mp map[K]V, key K) (exists bool)
- func MapUnset[K comparable, V any](mp map[K]V, key K)
- func Must(err error)
- func MustAssertType[T any](msg string, v any) T
- func MustBeNillable(typ reflect.Type)
- func MustConvertToMap[K comparable, V any](msg string, v any) map[K]V
- func MustConvertToSlice[T any](msg string, v any) []T
- func MustConvertToSlice2[T any](msg string, v any) [][]T
- func MustNonEmptySlice[T any](v []T) []T
- func MustNonNilValue[T any](val T) T
- func MustNonZero[T any](v T) T
- func MustValue[T any](t T, err error) T
- func MustValue2[T, U any](t T, u U, err error) (T, U)
- func MustValue3[T, U, V any](t T, u U, v V, err error) (T, U, V)
- func Nillable(typ reflect.Type) bool
- func Not[T any](filter func(T) bool) func(T) bool
- func Or[T any](filters ...func(T) bool) func(T) bool
- func OrderedTuple2Search[K constraint.Ordered, V any](mp []tuple.Two[K, V], key K) (int, bool)
- func OrderedTuple2SliceAdd[K constraint.Ordered, V any](mp *[]tuple.Two[K, []V], key K, value V)
- func OrderedTuple2SliceRemoveKey[K constraint.Ordered, V any](mp *[]tuple.Two[K, V], key K)
- func OrderedTuple2SliceRemoveUncomparable[K constraint.Ordered, V any](mp *[]tuple.Two[K, []V], key K, value V, all ...bool)
- func OrderedTuple2SliceRemoveValue[K constraint.Ordered, V comparable](mp *[]tuple.Two[K, []V], key K, value V, all ...bool)
- func SecondValue2[T, U any](t T, u U) U
- func SecondValue3[T, U, V any](t T, u U, v V) U
- func SliceAdd[T any](slc *[]T, value T)
- func SliceCopy[T any](slc []T) (res []T)
- func SliceFlatten[T any](value any) []T
- func SliceIndex[T any](slc []T, index int, defawlt ...T) T
- func SliceOf[T any](vals ...T) []T
- func SliceRemove[T comparable](slc []T, val T, all ...bool) []T
- func SliceRemoveUncomparable[T any](slc []T, val T, all ...bool) []T
- func SliceReverse[T any](slc []T) []T
- func SliceSortBy[T any](slc []T, less func(T, T) bool) []T
- func SliceSortCmp[T constraint.Cmp[T]](slc []T) []T
- func SliceSortComplex[T constraint.Complex](slc []T) []T
- func SliceSortOrdered[T constraint.Ordered](slc []T) []T
- func SliceToMap[T comparable](slc []T) (res map[T]bool)
- func SliceToMapBy[T any, K comparable](slc []T, fn func(T) K) (res map[K]bool)
- func SliceUniqueValues[T comparable](slc []T) []T
- func SnakeToCamelCase(snake string) string
- func SupplierCached[T any](supplier func() T) func() T
- func SupplierOf[T any](value T) func() T
- func Ternary[T any](expr bool, trueVal T, falseVal T) T
- func TernaryResult[T any](expr bool, trueVal func() T, falseVal func() T) T
- func ThirdValue3[T, U, V any](t T, u U, v V) V
- func TryTo(tryFn func(), panicFn func(any), closers ...func())
Constants ¶
const ( // The ALL constant is for some remove funcs ALL = true )
Variables ¶
This section is empty.
Functions ¶
func And ¶
And converts any number of filter funcs (func(T) bool) into the conjunction of all the funcs. Short-circuit logic will return false on the first function that returns false. If no filters are provided, the result is a function that always returns true.
func AssertType ¶
AssertType asserts that v is of type T - intended for cases where T is a scalar type returns (v type asserted to T, nil) if v is of type T returns (T zero value, error) if v is not of type T
func CamelCaseToSnakeCase ¶
CamelCaseToSnakeCase converts [cC]amelCase to snake_case
func Compose ¶
func Compose[T any](f0 func(T) T, fns ...func(T) T) func(T) T
Compose composes one or more funcs that accept and return the same type into a new function that returns f_n(f_n-1( ... (f_1(f_0(x))))). Eg, if three funcs f_0, f_1, f_2 are provided in that order, the resulting function returns f_2(f_1(f_0(x))).
func Compose10 ¶
func Compose10[P, Q, R, S, T, U, V, W, X, Y, Z any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, f5 func(U) V, f6 func(V) W, f7 func(W) X, f8 func(X) Y, f9 func(Y) Z, ) func(P) Z
Compose10 composes ten funcs into a new func that transforms (p -> ... -> z)
func Compose2 ¶
func Compose2[P, Q, R any]( f0 func(P) Q, f1 func(Q) R, ) func(P) R
Compose2 composes two funcs into a new func that transforms (p -> q -> r)
func Compose3 ¶
func Compose3[P, Q, R, S any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, ) func(P) S
Compose3 composes three funcs into a new func that transforms (p -> ... -> s)
func Compose4 ¶
func Compose4[P, Q, R, S, T any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, ) func(P) T
Compose4 composes four funcs into a new func that transforms (p -> ... -> t)
func Compose5 ¶
func Compose5[P, Q, R, S, T, U any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, ) func(P) U
Compose5 composes five funcs into a new func that transforms (p -> ... -> u)
func Compose6 ¶
func Compose6[P, Q, R, S, T, U, V any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, f5 func(U) V, ) func(P) V
Compose6 composes six funcs into a new func that transforms (p -> ... -> v)
func Compose7 ¶
func Compose7[P, Q, R, S, T, U, V, W any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, f5 func(U) V, f6 func(V) W, ) func(P) W
Compose7 composes seven funcs into a new func that transforms (p -> ... -> w)
func Compose8 ¶
func Compose8[P, Q, R, S, T, U, V, W, X any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, f5 func(U) V, f6 func(V) W, f7 func(W) X, ) func(P) X
Compose8 composes eight funcs into a new func that transforms (p -> ... -> x)
func Compose9 ¶
func Compose9[P, Q, R, S, T, U, V, W, X, Y any]( f0 func(P) Q, f1 func(Q) R, f2 func(R) S, f3 func(S) T, f4 func(T) U, f5 func(U) V, f6 func(V) W, f7 func(W) X, f8 func(X) Y, ) func(P) Y
Compose9 composes nine funcs into a new func that transforms (p -> ... -> y)
func ConvertToMap ¶
func ConvertToMap[K comparable, V any](msg string, v any) (res map[K]V, err error)
ConvertToMap asserts that the any value given is a map[K]any, and all values of the map are type V, converting to a map[K]V
func ConvertToSlice ¶
ConvertToSlice asserts that the any value given is a []any, and that all elements are type T, converting to a []T
func ConvertToSlice2 ¶
ConvertToSlice2 asserts that the any value given is a []any, containing elements of []any, containing elements of T, converting to a [][]T
func Equal ¶
func Equal[T comparable](val T) func(T) bool
Equal returns a filter func (func(T) bool) that returns true if it accepts a value that equals the given value with ==
func FirstValue2 ¶
func FirstValue2[T, U any](t T, u U) T
FirstValue2 takes two values and returns only the first one. Useful for functions that return two results and you only care about the first one
func FirstValue3 ¶
func FirstValue3[T, U, V any](t T, u U, v V) T
FirstValue3 takes three values and returns only the first one. Useful for functions that return three results and you only care about the first one
func GreaterThan ¶
func GreaterThan[T constraint.Ordered](val T) func(T) bool
GreaterThan returns a filter func (func(T) bool) that returns true if it accepts a value that is greater than the given value
func GreaterThanEqual ¶
func GreaterThanEqual[T constraint.Ordered](val T) func(T) bool
GreaterThanEqual returns a filter func (func(T) bool) that returns true if it accepts a value that is greater than or equal to the given value
func IgnoreResult ¶
func IgnoreResult[T any](fn func() T) func()
IgnoreResult takes a func of no args that returns any type, and generates a func of no args and no return value that invokes it.
Useful for TryTo function closers.
func In ¶
func In[T comparable](val ...T) func(T) bool
In returns a filter func (func(T) bool) that returns true if it accepts a value that equals any given value with ==
func IsNegative ¶
func IsNegative[T constraint.Signed]() func(T) bool
IsNegative returns a filter func (func(T) bool) that returns true if it accepts a negative value.
func IsNil ¶
IsNil generates a filter func (func(T) bool) that returns true if the value given is nil. A type constraint cannot be used to describe nillable types at compile time, so reflection is used. Panics if T is not a nillable type.
func IsNonNegative ¶
func IsNonNegative[T constraint.Signed]() func(T) bool
IsNonNegative returns a filter func (func(T) bool) that returns true if it accepts a non-negative value.
func IsNonNil ¶
IsNonNil generates a filter func (func(T) bool) that returns true if the value given is non-nil. A type constraint cannot be used to describe nillable types at compile time, so reflection is used. Panics if T is not a nillable type.
func IsPositive ¶
func IsPositive[T constraint.Signed]() func(T) bool
IsPositive returns a filter func (func(T) bool) that returns true if it accepts a positive value.
func LessThan ¶
func LessThan[T constraint.Ordered](val T) func(T) bool
LessThan returns a filter func (func(T) bool) that returns true if it accepts a value that is less than the given value
func LessThanEqual ¶
func LessThanEqual[T constraint.Ordered](val T) func(T) bool
LessThanEqual returns a filter func (func(T) bool) that returns true if it accepts a value that is less than or equal to the given value
func Map2Set ¶
func Map2Set[K1, K2 comparable, V any](mp *map[K1]map[K2]V, key1 K1, key2 K2, value V)
Map2Set makes accessing two level maps easier, by automatically creating the first and second maps as needed. Particularly useful for struct fields, as the zero value of the struct will have a nil map.
EG: var mp map[string]map[int]bool // nil map Map2Set(&mp, "foo", 3, true)
since mp is nil, sets mp = map[int]bool{} since mp["foo"] is nil, sets mp["foo"] = map[int]bool{} sets mp["foo"][3] = true
Map2Set(&mp, "foo", 4, false)
since mp and mp["foo"] both exist, sets mp["foo"][4] = false
func Map2SliceAdd ¶
func Map2SliceAdd[K1, K2 comparable, V any](mp *map[K1]map[K2][]V, key1 K1, key2 K2, value V)
Map2SliceAdd makes accessing a two level map whose value is a slice easier, by automatically creating the first and second maps and slice as needed. Particularly useful for struct fields, as the zero value of the struct will have a nil map.
EG: var mp map[string]map[int][]bool Map2SliceAdd(&mp, "foo", 3, true)
since mp is nil, sets mp = map[string]map[int][]bool{} since mp["foo"] is nil, sets mp["foo"] = map[int][]bool{} since mp["foo"][3] is nil, sets mp["foo"][3] = []bool{} appends true to map["foo"][3]
Map2SliceAdd(&mp, "foo", 3, false)
since mp, mp["foo"] and mp["foo"][3] all exist, appends false to mp["foo"][3]
func Map2SliceRemove ¶
func Map2SliceRemove[K1, K2, V comparable](mp map[K1]map[K2][]V, key1 K1, key2 K2, value V, all ...bool)
Map2SliceRemove makes accessing a two level map whose value is a slice easier, by removing a value from the slice if it exists. If the optional all is true, all occurrences of V are removed, otherwise only the first is removed.
func Map2SliceRemoveUncomparable ¶
func Map2SliceRemoveUncomparable[K1, K2 comparable, V any](mp map[K1]map[K2][]V, key1 K1, key2 K2, value V, all ...bool)
Map2SliceRemoveUncomparable is an uncomparable version of Map2SliceRemove
func Map2Test ¶
func Map2Test[K1, K2 comparable, V any](mp map[K1]map[K2]V, key1 K1, key2 K2) (exists bool)
Map2Test returns true if mp[K1] and mp[K1][K2] exist
func Map2Unset ¶
func Map2Unset[K1, K2 comparable, V any](mp map[K1]map[K2]V, key1 K1, key2 K2)
Map2Unset makes accessing two level maps easier, by removing K2 from second level map.
EG: var mp map[string]map[int]bool Map2Set(&mp, "foo", 3, true) Map2Set(&mp, "foo", 4, false) Map2Unset(&mp, "foo", 3) -> only mp["foo"][4] exists
func MapIndex ¶
func MapIndex[K comparable, V any](mp map[K]V, key K, defawlt ...V) V
MapIndex returns the first of the following: 1. map[key] if the map is non-nil and the key exists in the map 2. default if provided 3. zero value of map value type
func MapKeysToSlice ¶
func MapKeysToSlice[K comparable, V any](mp map[K]V) []K
MapKeysToSlice collects the map keys into a slice, for scenarios that require a slice of unique values only. If the map is empty, an empty slice is returned; the result is never nil.
func MapSet ¶
func MapSet[K comparable, V any](mp *map[K]V, key K, value V)
MapSet makes accessing maps easier, by automatically creating the map as needed. Particularly useful for struct fields, as the zero value of the struct will have a nil map.
EG: var mp map[string]int // nil map MapSet(&mp, "foo", 3)
since mp is nil, sets *mp = map[string]{} sets *mp["foo"] = 3
MapSet(&mp, "foo", 4)
since *mp exists, sets *mp["foo"] = 4
func MapSliceAdd ¶
func MapSliceAdd[K comparable, V any](mp *map[K][]V, key K, value V)
MapSliceAdd makes accessing a map whose value is a slice easier, by automatically creating the map and slice as needed. Particularly useful for struct fields, as the zero value of the struct will have a nil map.
EG: var mp map[string][]int MapSliceAdd(&mp, "foo", 3)
since mp is nil, sets mp = map[string][]int{} since mp["foo"] is nil, sets mp["foo"] = []int{} appends 3 to map["foo"]
MapSliceAdd(&mp, "foo", 4)
since mp and mp["foo"] both exist, appends 4 to mp["foo"]
func MapSliceRemove ¶
func MapSliceRemove[K, V comparable](mp map[K][]V, key K, value V, all ...bool)
MapSliceRemove makes accessing a map whose value is a slice easier, by removing a value from the slice if it exists. If the optional all is true, all occurrences of V are removed, otherwise only the first is removed.
func MapSliceRemoveUncomparable ¶
func MapSliceRemoveUncomparable[K comparable, V any](mp map[K][]V, key K, value V, all ...bool)
MapSliceRemoveUncomparable is an uncomparable version of MapSliceRemove
func MapSortBy ¶
func MapSortBy[K comparable, V any](mp map[K]V, less func(K, K) bool) []tuple.Two[K, V]
MapSortBy sorts a map with any type of key into a []Two[K, V]
func MapSortCmp ¶
func MapSortCmp[K constraint.Cmp[K], V any](mp map[K]V) []tuple.Two[K, V]
MapSortCmp sorts a map with a Cmp key into a []Two[K, V]
func MapSortComplex ¶
func MapSortComplex[K constraint.Complex, V any](mp map[K]V) []tuple.Two[K, V]
MapSortComplex sorts a map with a Complex key into a []Two[K, V]
func MapSortOrdered ¶
func MapSortOrdered[K constraint.Ordered, V any](mp map[K]V) []tuple.Two[K, V]
MapSortOrdered sorts a map with an Ordered key into a []Two[K, V]
func MapTest ¶
func MapTest[K comparable, V any](mp map[K]V, key K) (exists bool)
MapTest returns true if the map is non-nil and contains the given key
func MapUnset ¶
func MapUnset[K comparable, V any](mp map[K]V, key K)
MapUnset is the reverse of MapSet, for consistency
func Must ¶
func Must(err error)
Must panics if the error is non-nil, else returns. Useful to wrap calls to functions that return only an error.
func MustAssertType ¶
MustAssertType is a must version of AssertType
func MustBeNillable ¶
MustBeNillable panics if Nillable(typ) returns false
func MustConvertToMap ¶
func MustConvertToMap[K comparable, V any](msg string, v any) map[K]V
MustConvertToMap is a must version of ConvertToMap
func MustConvertToSlice ¶
MustConvertToSlice is a must version of ConvertToSlice
func MustConvertToSlice2 ¶
MustConvertToSlice2 is a must version of ConvertToSlice2
func MustNonEmptySlice ¶
func MustNonEmptySlice[T any](v []T) []T
MustNonEmptySlice panics if the value passed is a nil or empty slice
func MustNonNilValue ¶
func MustNonNilValue[T any](val T) T
MustNonNilValue returns the value if it is non-nil, else panics Panics if the value is not a nillable type
func MustNonZero ¶
func MustNonZero[T any](v T) T
MustNonZero panics if the value passed is a zero value, else it returns the value passed
func MustValue ¶
MustValue panics if the error is non-nil, else returns the value of type T. Useful to wrap calls to functions that return a value and an error, where the value is only valid if the error is nil.
func MustValue2 ¶
MustValue2 panics if the error is non-nil, else returns the values of types T and U. Useful to wrap calls to functions that return two values and an error, where the values are only valid if the error is nil.
func MustValue3 ¶
MustValue3 panics if the error is non-nil, else returns the values of types T, U, and V. Useful to wrap calls to functions that return two values and an error, where the values are only valid if the error is nil.
func Nillable ¶
Nillable returns true if the given reflect.Type represents a chan, func, map, pointer, or slice.
func Or ¶
Or converts any number of filter funcs (func(T) bool) into the disjunction of all the funcs. Short-circuit logic will return true on the first function that returns true. If no filters are provided, the result is a function that always returns true.
func OrderedTuple2Search ¶
OrderedTuple2Search searches a []Tuple.Two[K, V] for a Tuple.Two whose K value is the one provided. The result is the index of the largest K that is <= the K provided, and true if it is an exact match. When false is returned, it means if the given K is to be inserted, it needs to be inserted after the returned index.
If the slice is nil or empty, then the result is -1, false ¶
An ordered []Tuple.Two[K, V] can be used as a sorted map[K]V
func OrderedTuple2SliceAdd ¶
func OrderedTuple2SliceAdd[K constraint.Ordered, V any](mp *[]tuple.Two[K, []V], key K, value V)
OrderedTuple2SliceAdd searches a []Tuple.Two[K, []V] for a tuple with the given key K. If such a tuple exists, the value V is added to the end of the slice. If no such tuple exists, a new tuple is inserted with a slice containing only the value V provided.
func OrderedTuple2SliceRemoveKey ¶
func OrderedTuple2SliceRemoveKey[K constraint.Ordered, V any](mp *[]tuple.Two[K, V], key K)
OrderedTuple2SliceRemoveKey removes a key from the []Tuple.Two[K, V], if the key exists
func OrderedTuple2SliceRemoveUncomparable ¶
func OrderedTuple2SliceRemoveUncomparable[K constraint.Ordered, V any](mp *[]tuple.Two[K, []V], key K, value V, all ...bool)
OrderedTuple2SliceRemoveUncomparable removes an uncomparable value from the []Tuple.Two[K, []V], if the key exists and the slice contains the value. If the optional all is true, then all occurrences of the value are removed, else the first occurrence is removed.
func OrderedTuple2SliceRemoveValue ¶
func OrderedTuple2SliceRemoveValue[K constraint.Ordered, V comparable](mp *[]tuple.Two[K, []V], key K, value V, all ...bool)
OrderedTuple2SliceRemoveValue removes a value from the []Tuple.Two[K, []V], if the key exists and the slice contains the value. If the optional all is true, then all occurrences of the value are removed, else the first occurrence is removed.
func SecondValue2 ¶
func SecondValue2[T, U any](t T, u U) U
SecondValue2 takes two values and returns only the second one. Useful for functions that return two results and you only care about the second one
func SecondValue3 ¶
func SecondValue3[T, U, V any](t T, u U, v V) U
SecondValue3 takes three values and returns only the second one. Useful for functions that return three results and you only care about the second one
func SliceAdd ¶
func SliceAdd[T any](slc *[]T, value T)
SliceAdd makes adding values to slices easier, by automatically creating the slice as needed. Particularly useful for struct fields, as the zero value of the struct will have a nil slice.
EG: var slc []int // nil slice SliceAdd(&slc, 3)
since slc is nil, sets *slc = []int{} appends 3 to *slc
SliceAdd(&slc, 4)
since *slc exists, appends 4 to *slc
func SliceCopy ¶
func SliceCopy[T any](slc []T) (res []T)
SliceCopy returns a copy of a slice, useful for situations such as sorting a copy of a slice without modifying the original. If the original slice is null, the result is empty.
func SliceFlatten ¶
SliceFlatten flattens a slice of any number of dimensions into a one dimensional slice. The slice is received as type any, because there is no way to describe a slice of any number of dimensions using generics.
If a nil value is passed, an empty slice is returned.
Panics if: - the value passed is not a slice - the slice passed does not ultimately resolve to elements of type T once all slice dimensions are indexed
func SliceIndex ¶
SliceIndex returns the first of the following given a slice, index, and optional default value: 1. slice[index] if the slice is non-nil and length > index 2. slice[length + index] if the slice is non-nil and index < 0 and length + index >= 0 3. default value if provided 4. zero value of slice element type
func SliceOf ¶
func SliceOf[T any](vals ...T) []T
SliceOf allows caller to infer the slice type rather than have to write it out. This is useful when the type is a more lengthy declaration.
func SliceRemove ¶
func SliceRemove[T comparable](slc []T, val T, all ...bool) []T
SliceRemove removes a slice element from a slice. By default, only the first occurrence is removed. If the option all param is true, then all occurrences are removed. The new slice is returned.
Note: If only the first occurrence is removed, then the append builtin is used twice, once with all elements before the occurrence, and again fior all elelemts after it. Otherwise, a new slice is populated with all other elements.
func SliceRemoveUncomparable ¶
SliceRemoveUncomparable removes a slice element from a slice of uncomparable values (they must be pointers). By default, only the first occurrence is removed. If the optional all param is true, then all occurrences are removed. The new slice is returned.
Note: If only the first occurrence is removed, then the the append builtin is used twice, once with all elements before the occurrence, and again for all elements after it. Otherwise, a new slice is populated with all other elements.
func SliceReverse ¶
func SliceReverse[T any](slc []T) []T
SliceReverse reverses the elements of a slice, so that [1,2,3] becomes [3,2,1]. The slice is modified in place, and returned.
func SliceSortBy ¶
SliceSortBy sorts a slice of any type with the provided comparator. The slice is modified in place, and returned.
func SliceSortCmp ¶
func SliceSortCmp[T constraint.Cmp[T]](slc []T) []T
SliceSortCmp sorts a slice of Cmp. The slice is modified in place, and returned.
func SliceSortComplex ¶
func SliceSortComplex[T constraint.Complex](slc []T) []T
SliceSortComplex sorts a slice of Complex. The slice is modified in place, and returned.
func SliceSortOrdered ¶
func SliceSortOrdered[T constraint.Ordered](slc []T) []T
SliceSortOrdered sorts a slice of Ordered. The slice is modified in place, and returned.
func SliceToMap ¶
func SliceToMap[T comparable](slc []T) (res map[T]bool)
SliceToMap copies slice elements to map keys, where the value of each key is true, to avoid the need for two arg map lookups to see if a key exists. If the slice is nil or empty, an empty map is returned.
func SliceToMapBy ¶
func SliceToMapBy[T any, K comparable](slc []T, fn func(T) K) (res map[K]bool)
SliceToMapBy transforms slice elements to map keys using provided func, where the value of each key is true, to avoid the need for two arg map lookups to see if a key exists. If the slice is nil or empty, an empty map is returned.
func SliceUniqueValues ¶
func SliceUniqueValues[T comparable](slc []T) []T
SliceUniqueValues returns the uniq values of a slice, in no particular order See MapKeysToSlice
func SnakeToCamelCase ¶
SnakeToCamelCase converts [sS]nake_[cC]ase to CamelCase If the input is empty, the result is empty
func SupplierCached ¶
func SupplierCached[T any](supplier func() T) func() T
SupplierCached generates a func() T that caches the result of the given supplier on the first call. Any subseqquent calls return the cached value, guaranteeing the provided supplier is invoked at most once.
func SupplierOf ¶
func SupplierOf[T any](value T) func() T
SupplierOf generates a func() T that returns the given value every time it is called
func TernaryResult ¶
TernaryResult returns trueVal() if expr is true, else it returns falseVal() Either function can be nil if it is not relevant, in which case the zero value of T is returned
func ThirdValue3 ¶
func ThirdValue3[T, U, V any](t T, u U, v V) V
ThirdValue3 takes three values and returns only the third one. Useful for functions that return three results and you only care about the third one
func TryTo ¶
func TryTo(tryFn func(), panicFn func(any), closers ...func())
TryTo executes tryFn, and if a panic occurs, it executes panicFn. If any closers are provided, they are deferred in the provided order before the tryFn, to ensure they get closed even if a panic occurs. If any closer returns a non-nil error, any remaining closers are still called, as that is go built in behaviour.
This function simplifies the process of "catching" panics over using reverse order code like the following (common in unit tests that want to verify the type of object sent to panic):
func DoSomeStuff() { ... func() { defer zero or more things that have to be closed before we try to recover from any panic defer func() { // Some code that uses recover() to try and deal with a panic }() // Some code that may panic, which is handled by above code } ... }
Types ¶
This section is empty.