Documentation
¶
Index ¶
- func Equal[T any](a, b T) bool
- func GetMaxStringLen(values []string) int
- func IsNil(value any) bool
- func IsZero[T any](value T) bool
- func Keys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TKey
- func LazyMatcher(pattern string) func(value string) bool
- func LazyRegex(pattern string) func() *regexp.Regexp
- func Length[T any](value T) (length int, ok bool)
- func Parse[T ParsableConstraint](s string) (T, error)
- func RemoveZeros[T any, S ~[]T](s S) S
- func SetToZero[T any, S ~[]T](s S, start, stop int)
- func SortedKeys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap, comparer ...Comparer[TKey]) []TKey
- func String[T any](value T) string
- func Strings[T any, S ~[]T](s S) []string
- func TryIsNil(value any) (isNil, ok bool)
- func TypeOf[T any]() reflect.Type
- func Values[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TValue
- func Zero[T any]() T
- type Comparable
- type Comparer
- func ComparableComparer[T Comparable[T]]() Comparer[T]
- func ComparerForLess[T any](less IsLessThan[T]) Comparer[T]
- func DefaultComparer[T any]() Comparer[T]
- func Descender[T any](cmp Comparer[T]) Comparer[T]
- func DurationComparer() Comparer[time.Duration]
- func EpsilonComparer[T NumConstraint](epsilon T) Comparer[T]
- func OrderedComparer[T cmp.Ordered]() Comparer[T]
- func TimeComparer() Comparer[time.Time]
- type Equatable
- type FloatingConstraint
- type IntConstraint
- type IsLessThan
- type NumConstraint
- type ParsableConstraint
- type StringMatcher
- type Stringer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal determines if the two values are equal.
This will check if the objects are Equatable, otherwise it will fallback to a DeepEqual. This will not check for Equatable within a slice, array, map, etc only in the top level object.
This will not check for Comparable types. Any struct that implements Comparable should also implement Equatable where both agree.
func GetMaxStringLen ¶
GetMaxStringLen gets the maximum length of the given strings.
func IsNil ¶
IsNil determines if the value is nil or returns false if the value isn't able to check for nil.
func IsZero ¶
IsZero determines if the given value is equivalent to the zero value of the given type.
This will also return true for any nil type values.
func Keys ¶
func Keys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TKey
Keys gets all the keys for the given map in random order.
func LazyMatcher ¶
LazyMatcher is a regular expression matcher which lazy compiles the pattern until the first time it is needed.
If the pattern is invalid, this will panic the first time it is called to perform a match. It will panic the same error for any following match attempts.
Example:
var hex = LazyMatcher(`^[0-9A-Fa-f]+$`) func Foo() { ... isHex := hex(`572A6F`) // true ... }
func LazyRegex ¶
LazyRegex is a regular expression which lazy compiles the pattern until the first time it is needed.
If the pattern is invalid, this will panic the first time it is called to get the regex. It will panic the same error for any following attempts.
func Length ¶
Length determines the length of the given value or returns false if the value does not have a length.
func Parse ¶
func Parse[T ParsableConstraint](s string) (T, error)
Parse interprets a string and returns the corresponding value of the given type.
func RemoveZeros ¶
func RemoveZeros[T any, S ~[]T](s S) S
RemoveZeros creates a new slice without modifying the given values which has all the zero values remove from it.
func SetToZero ¶
SetToZero sets the given range of the slice to zero. The start is inclusive, the stop is exclusive.
This will panic if the indices are not valid. The start must be `[0..len)` and stop must be `(start..len]`.
func SortedKeys ¶
func SortedKeys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap, comparer ...Comparer[TKey]) []TKey
SortedKeys gets all the keys from the given map in sorted order.
An optional comparer maybe added to override the default comparer or for types that don't have a default comparer.
func TryIsNil ¶
TryIsNil determines if the value is nil or returns false if the value isn't able to check for nil.
func Values ¶
func Values[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TValue
Values gets all the values for the given map in random order.
Types ¶
type Comparable ¶
type Comparable[T any] interface { // CompareTo returns a comparison result of this object and the given object. // // The comparison results should be: // `< 0` if this is less than the given other, // `== 0` if this equals the given other, // `> 0` if this is greater than the given other. CompareTo(other T) int }
Comparable is an object which can be compared against another object.
The given T variable type is typically the type that is implementing this interface.
type Comparer ¶
Comparer returns the comparison result between the two given values.
The comparison results should be: `< 0` if x is less than y, `== 0` if x equals y, `> 0` if x is greater than y.
func ComparableComparer ¶
func ComparableComparer[T Comparable[T]]() Comparer[T]
ComparableComparer returns a comparer which compares the given comparable type.
func ComparerForLess ¶
func ComparerForLess[T any](less IsLessThan[T]) Comparer[T]
LessComparer returns a comparer which compares two values using a IsLessThan function to perform the comparison.
func DefaultComparer ¶
DefaultComparer tries to create a comparer for the given type. If a comparer isn't able to be created, this will return nil.
func Descender ¶
Descender returns a comparer which negates the given comparer.
Typically negating a comparer will change a sort from ascending to descending.
func DurationComparer ¶
DurationComparer returns a comparer which compares the given time duration.
func EpsilonComparer ¶
func EpsilonComparer[T NumConstraint](epsilon T) Comparer[T]
EpsilonComparer returns an epsilon comparer which compares the given floating point types.
An epsilon comparator should be used when comparing calculated floating point numbers since calculations may accrue small errors and make the actual value very close to the expected value but not exactly equal. The two floating point values are equal if very close to each other. The values must be within the given epsilon to be considered equal.
The given epsilon must be greater than zero. If the epsilon is less than or equal to zero, this will fallback to an ordered comparer.
func OrderedComparer ¶
OrderedComparer returns a comparer which compares the given ordered type.
func TimeComparer ¶
TimeComparer returns a comparer which compares the given time duration.
type Equatable ¶
type Equatable interface { // Equals returns true if this object and the given object are equal. Equals(other any) bool }
Equatable is an object which can be checked for equality against another object.
type FloatingConstraint ¶
FloatingConstraint is a type constraint for floating point types.
type IntConstraint ¶
type IntConstraint interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 }
IntConstraint is a type constraint for integer types.
type IsLessThan ¶
IsLessThan returns true if the given x is less than the given y, otherwise false.
type NumConstraint ¶
type NumConstraint interface { IntConstraint | FloatingConstraint }
NumConstraint is a type constraint for numerical types.
type ParsableConstraint ¶
type ParsableConstraint interface { ~string | ~bool | NumConstraint | ~complex64 | ~complex128 }
ParsableConstraint is the set of types that can be parsed.
type StringMatcher ¶
StringMatcher is a function for finding the first match in the given string.
This returns the starting index of the first character (UTF-8) in the match and the length of the matched substring. If no match found then start should return -1. If a match is found the length must be greater than zero. For a match, the start index plus the length should not go past the end of the given value.