Documentation ¶
Index ¶
- Variables
- func Avg[T constraints.Integer | constraints.Float](items ...T) T
- func CloseChar(openChar rune) (rune, error)
- func CollectPointers[T any](arr []T) []*T
- func CollectPointersIf[T any](arr []T, filterFunc func(elem T) bool) []*T
- func CompareInterfaceValues[T comparable](a, b interface{}) (bool, error)
- func Contains[T comparable](arr []T, item T) bool
- func CopySlice[T any](arr []T) []T
- func DefaultValue[T interface{}]() T
- func DerefValues[T any](arr []*T) []T
- func DerefValuesIf[T any](arr []*T, filterFunc func(elem T) bool) []T
- func FilterSlice[T any](arr []T, filterFunc func(elem T) bool) []T
- func GroupConsecutive[N GoNumber](s []N) [][2]N
- func GroupConsecutiveSorted[N GoNumber](s []N) [][2]N
- func InterfaceTo[T any](v interface{}) (T, error)
- func IsCloseChar(closeChar rune) bool
- func IsOpenChar(openChar rune) bool
- func IsWellClosed(s string) error
- func Map[T any, U any](arr []T, mapFunc func(elem T) (U, bool)) []U
- func Max[T constraints.Ordered](items ...T) T
- func Min[T constraints.Ordered](items ...T) T
- func OpenChar(closeChar rune) (rune, error)
- func Retry(action string, f func() error, opts ...RetryOption) error
- func RetryWithReturn[T any](action string, f func() (T, error), opts ...RetryOption) (T, error)
- func Reverse[T any](arr []T) []T
- func ReverseInPlace[T any](arr []T)
- func SliceFromMapKeys[K comparable, V any](m map[K]V) []K
- func SliceFromMapKeysIf[K comparable, V any](m map[K]V, f func(K, V) bool) []K
- func SliceFromMapValues[K comparable, V any](m map[K]V) []V
- func SliceFromMapValuesIf[K comparable, V any](m map[K]V, f func(K, V) bool) []V
- func SlicesFromMap[K comparable, V any](m map[K]V) ([]K, []V)
- func SlicesFromMapIf[K comparable, V any](m map[K]V, f func(K, V) bool) ([]K, []V)
- func StringerToLowerString(s stringer) string
- func StringerToUpperString(s stringer) string
- func Sum[T constraints.Integer | constraints.Float](items ...T) T
- func ToLower[T ~string](s T) T
- func ToUpper[T ~string](s T) T
- func ZeroedValue[T any]() T
- type GoNumber
- type RetryOption
Constants ¶
This section is empty.
Variables ¶
var ErrNotConvertible = errors.New("types are not convertible")
var ErrRetry = errors.New("error when retrying")
Functions ¶
func Avg ¶
func Avg[T constraints.Integer | constraints.Float](items ...T) T
func CollectPointers ¶
func CollectPointers[T any](arr []T) []*T
CollectPointers iterates over |arr| and returns a new slice containing all references to the elements of |arr|.
func CollectPointersIf ¶
CollectPointersIf iterates over |arr| and calling filterFunc on the element |elem|. If filterFunc returns true, |elem| is added to the return slice.
func CompareInterfaceValues ¶
func CompareInterfaceValues[T comparable](a, b interface{}) (bool, error)
CompareInterfaceValues will compares |a| and |b| as type T. It returns true if |a| and |b| are of type T and are equal in value, or if |a| and |b| can be converted to T and are equal in value. If |a| and |b| are not equal but can be converted to T, then it returns false. CompareInterfaceValues *only* returns non-nil error if any of the values cannot be converted into T.
func Contains ¶
func Contains[T comparable](arr []T, item T) bool
Contains iterates over |arr| and returns true if an element in |arr| is equal to |item|.
func DefaultValue ¶
func DefaultValue[T interface{}]() T
DefaultValue is just an alias to ZeroedValue
func DerefValues ¶
func DerefValues[T any](arr []*T) []T
DerefValues iterates over |arr| and return a new slice containing dereferenced values of the elements in |arr|. If the pointer at `arr[i]` is nil, the returned slice will have default value of type `T` at index `i`.
func DerefValuesIf ¶
DerefValuesIf iterates over |arr| and calls |filterFunc| on the element |elem|. If filterFunc returns true, |elem|'s dereferenced value is added to the return slice. Unlike DerefValues but similar to CollectPointersIf, if |elem| is nil, its value will not be added to the return slice
func FilterSlice ¶
FilterSlice iterates over |arr| and calls |filterFunc| on each of the element |elem|. If filterFunc returns true, |elem| is added to the return slice.
func GroupConsecutive ¶
func GroupConsecutive[N GoNumber](s []N) [][2]N
GroupConsecutive *sorts* slice |s| in-place, and calls GroupConsecutiveSorted on |s|.
func GroupConsecutiveSorted ¶
func GroupConsecutiveSorted[N GoNumber](s []N) [][2]N
GroupConsecutiveSorted groups input numbers in slice []N |s| that are consecutive to each other (i.e. difference of 1) into a slice of [2]N. The result type [2]N represents the start and end of a consecutive range, that is, result[0] is "from" while result[1] is "to" of such ranges.
If a breakpoint (non-consecutive element) is found, GroupConsecutive creates a new result. e.g. [1, 2, 3, 5, 6, 8, 9, 10] will be mapped to [{1, 3}, {5, 6}, {8, 10}].
If |s| has length of 0, it returns [][2]N{ {0, 0} }. If |s| has length of 1 and is []N{n}, it returns [][2]N{ {n, n} }.
func InterfaceTo ¶
InterfaceTo converts v from interface{} to T. It returns a zeroed T and an error if not convertible.
func IsCloseChar ¶
func IsOpenChar ¶
func IsWellClosed ¶
IsWellClosed validates if all surrounds are properly closed
Examples (see unit test) ¶
1. ([bar(foo)]) -> ok
2. [()[{}]] -> ok
3. [((] -> error
4. [) -> error
5. [ -> error
Notes: Only openning/closing characters are concerned.
func Map ¶
Map maps |arr| to a new slice using |mapFunc|. |mapFunc| returns a tuple of generic type U and a boolean. If |mapFunc|'s 2nd return value is true, its 1st return value (of type U) will be appended to the result slice. Note: `T` and `U` can be of the same type.
func Max ¶
func Max[T constraints.Ordered](items ...T) T
func Min ¶
func Min[T constraints.Ordered](items ...T) T
func Retry ¶
func Retry(action string, f func() error, opts ...RetryOption) error
Retry wraps retry with action string.
func RetryWithReturn ¶
func RetryWithReturn[T any]( action string, f func() (T, error), opts ...RetryOption, ) ( T, error, )
RetryWithReturn wraps |f| in a `func() error` and captures the T value in that function, and returns T returned by |f|.
func Reverse ¶
func Reverse[T any](arr []T) []T
Reverse returns a new slice, with elements being those of |arr| after being reversed.
func SliceFromMapKeys ¶
func SliceFromMapKeys[K comparable, V any](m map[K]V) []K
SliceFromMapKeys collects map[K]V |m| into []K. If |m| is mil, SliceFromMapKeys returns nil.
func SliceFromMapKeysIf ¶
func SliceFromMapKeysIf[K comparable, V any](m map[K]V, f func(K, V) bool) []K
SliceFromMapKeysIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the key in |m| gets appended to the return slice. If either |m| or |f| is nil, SliceFromMapKeysIf returns nil.
func SliceFromMapValues ¶
func SliceFromMapValues[K comparable, V any](m map[K]V) []V
SliceFromMapValues collects map[K]V |m| into []V. If |m| is mil, SliceFromMapValues returns nil
func SliceFromMapValuesIf ¶
func SliceFromMapValuesIf[K comparable, V any](m map[K]V, f func(K, V) bool) []V
SliceFromMapValuesIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the value in |m| gets appended to the return slice. If either |m| or |f| is nil, SliceFromMapValuesIf returns nil.
func SlicesFromMap ¶
func SlicesFromMap[K comparable, V any](m map[K]V) ([]K, []V)
SlicesFromMap collects map[K]V |m| into ([]K, []V). If |m| is nil, SlicesFromMap returns nil.
func SlicesFromMapIf ¶
func SlicesFromMapIf[K comparable, V any](m map[K]V, f func(K, V) bool) ([]K, []V)
SlicesFromMapIf iterates over |m| and calls |f| with keys and values of |m|. For each true returned from |f|, the key and value in |m| gets appended to both the return slices. If either |m| or |f| is nil, SlicesFromMapIf returns nil, nil.
func StringerToLowerString ¶
func StringerToLowerString(s stringer) string
StringerToLowerString calls String() on s and returns the result of calling `strings.ToLower` on that string.
func StringerToUpperString ¶
func StringerToUpperString(s stringer) string
StringerToUpperString calls String() on s and returns the result of calling `strings.ToUpper` on that string.
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](items ...T) T
Types ¶
type GoNumber ¶
type GoNumber interface { constraints.Integer | constraints.Float }
type RetryOption ¶
type RetryOption func(*retryConfig)
RetryOption is a function that takes in (and modifies) *Config
func LastErrorOnly ¶
func LastErrorOnly(lastErrOnly bool) RetryOption
LastErrorOnly sets retry option to only return the last error
func StopOnError ¶
func StopOnError(err error) RetryOption
StopOnError sets a specific error, which, if seen when retrying, breaks the retry loop. If |err| is found during retry, the loop breaks, and it is treated just like any other error, i.e. the retry still fails.