Documentation
¶
Index ¶
- func Flip[T any](test bool, a, b T) (T, T)
- func GetMaxStringLen(values []string) int
- func Is[T any](v any, out *T) bool
- 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 ...comp.Comparer[TKey]) []TKey
- func String[T any](value T) string
- func Strings[T any, S ~[]T](s S) []string
- func Ternary[T any](test bool, a, b T) T
- 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 FloatingConstraint
- type IntConstraint
- type NumConstraint
- type ParsableConstraint
- type StringMatcher
- type Stringer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flip ¶ added in v0.5.1
Flip returns `[b, a]` if the test is true, otherwise the values are returned in the given order, `[a, b]`.
Example: `max, min := Flip(x < y, x, y)`
func GetMaxStringLen ¶
GetMaxStringLen gets the maximum length of the given strings.
func Is ¶ added in v0.5.4
Is tries to cast the given value `v` into the given type. If `out` is not nil, then it will set with the cast value. Returns true if successfully cast, false otherwise.
This is designed to allow for inlining a cast into an if-statement without having to start another if-statement for each cast.
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 ...comp.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 Ternary ¶ added in v0.5.1
Ternary returns the `a` value if the given test is true, and the `b` value if the test is false.
Since both the `a` and `b` values are evaluated prior to the ternary, this should only be used when neither takes long to compute or with functions.
For example `value := Ternary(x, 1, -1)` or
value := Ternary(x, func() int { return foo() - 1 }, func() int { return bar()*6 + 2 }, )()
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 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 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.