Documentation
¶
Overview ¶
Package common contains common constants, functions and errors used throughout the flex codebase.
Index ¶
- Variables
- func CheckRange(start, end, step, length int) (err error)
- func Contains(entry, value any) bool
- func ConvertMapToLists(entry map[any]any) (keys, values []any, length int)
- func ConvertStringToList(entry string) (output []any)
- func CopyList(entry reflect.Value, length int) (output []any)
- func CopyMap(entry reflect.Value, capacity int) (output map[any]any)
- func Count(entry, value any) (count int)
- func Equal(a, b any) (equal bool)
- func GetMapInitialCapacity(elementCount int) int
- func IsInputFuncValid(f any, inputCount, outputCount int) error
- func IsJudgeFunc(f any) (err error)
- func IsList(entry any) error
- func IsSequence(entry any) error
- func Len(entry any) (length int)
- func ParseIndex(index, length int) int
- func WillReHash(oldElementCount, newElementCount int) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
ArrayType incluedes array and slice.
var ErrEmptyDict = errors.New("the input dict is empty")
ErrEmptyDict is an error indicating that the input dict is empty while it is expected to have at least one key-value pair.
var ErrEmptyList = errors.New("the input list is empty")
ErrEmptyList is an error indicating that the input list is empty while it is expected to have at least one element.
var ErrEmptySet = errors.New("the input set is empty")
ErrEmptySet is an error indicating that the input set is empty while it is expected to have at least one element.
var ErrInvalidCapacity = errors.New("the capacity must be a positive integer")
ErrInvalidCapacity is an error indicating that the capacity is not a positive integer as expected.
var ErrInvalidRange = errors.New("the range is invalid with the step: [start < end and step < 0] or [start > end and step > 0]")
ErrInvalidRange is an error indicating that the range is invalid with the step.
var ErrKeyNotFound = errors.New("the key is not found in the dict")
ErrKeyNotFound is an error indicating that the key is not found in the dict as expected.
var ErrListLengthMismatch = errors.New("the length of the input lists are not equal")
ErrListLengthMismatch is an error indicating that the length of the input lists are not fully the same as expected.
var ErrNotFunc = errors.New("the input parameter is not a function")
ErrNotFunc is an error indicating that the input parameter is not a function as expected.
var ErrNotIterable = errors.New("the input parameter is not iterable")
ErrNotIterable is an error indicating that the input parameter is not iterable as expected.
var ErrNotJudgeFunc = errors.New("the input parameter is not a function returning a boolean value")
ErrNotJudgeFunc is an error indicating that the input parameter is not a function returning a boolean value as expected.
var ErrNotList = errors.New("the input parameter must be a slice or array")
ErrNotList is an error indicating that the input parameter is not a slice or array as expected.
var ErrNotSeq = errors.New("the input parameter is not a slice, array or string")
ErrNotSeq is an error indicating that the input parameter is not a slice, array or string as expected.
var ErrOutOfRange = errors.New("the index is out of range")
ErrOutOfRange is an error indicating that the index is out of range.
var ErrTooManyArguments = errors.New("too many arguments")
ErrTooManyArguments is an error indicating that there are more arguments than expected.
var ErrUnexpectedParamCount = errors.New("unexpected number of input parameters")
ErrUnexpectedParamCount is an error indicating that the number of input parameters is unexpected.
var ErrUnexpectedReturnCount = errors.New("unexpected number of return values")
ErrUnexpectedReturnCount is an error indicating that the number of return values is unexpected.
var ErrZeroStep = errors.New("the step cannot be zero")
ErrZeroStep is an error indicating that the step is zero while it is expected to be a non-zero integer.
var IterableContainers = append(SequenceType, reflect.Map)
IterableContainers includes array, slice, string and map.
var SequenceType = append(ArrayType, reflect.String)
SequenceType includes array, slice and string.
Functions ¶
func CheckRange ¶
CheckRange checks if the range is valid and within the range of the length.
func Contains ¶
Contains checks if the input parameter contains the value, it can handle any type of values.
Example ¶
// string fmt.Println(Contains("hello", "l")) fmt.Println(Contains("hello", "x")) // slice fmt.Println(Contains([]int{1, 2, 3}, 2)) fmt.Println(Contains([]int{1, 2, 3}, 4)) // array fmt.Println(Contains([3]int{1, 2, 3}, 2)) fmt.Println(Contains([5]int{1, 2, 3}, 4)) // map fmt.Println(Contains(map[string]int{"a": 1, "b": 2}, "a")) fmt.Println(Contains(map[string]int{"a": 1, "b": 2}, 1))
Output: true false true false true false false true
func ConvertMapToLists ¶
ConvertMapToLists converts a map to a key list and a value list, and returns the length of the map.
func ConvertStringToList ¶
ConvertStringToList converts a string to a list of characters.
func Count ¶
Count counts the number of occurrences of the value in the input parameter, it can handle any type of values.
Example ¶
// string fmt.Println(Count("hello", "l")) // slice fmt.Println(Count([]int{1, 2, 3}, 2)) // array fmt.Println(Count([4]int{1, 2, 3, 2}, 2)) // map fmt.Println(Count(map[string]int{"a": 1, "b": 2}, "a")) fmt.Println(Count(map[string]int{"a": 1, "b": 2}, 1))
Output: 2 1 2 0 1
func Equal ¶
Equal checks if two values are equal, it can safely handle any type of values.
Example ¶
// map fmt.Println(Equal(map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 2})) fmt.Println(Equal(map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 3})) // slice fmt.Println(Equal([]int{1, 2, 3}, []int{1, 2, 3})) fmt.Println(Equal([]int{1, 2, 3}, []int{1, 2, 4})) // struct fmt.Println(Equal(struct{ a int }{1}, struct{ a int }{1})) fmt.Println(Equal(struct{ a int }{1}, struct{ a int }{2}))
Output: true false true false true false
func GetMapInitialCapacity ¶
GetMapInitialCapacity calculates the initial capacity of a map based on the expected number of elements.
func IsInputFuncValid ¶
IsInputFuncValid checks if the input parameter is a valid function and if it has the expected number of input and output parameters.
func IsJudgeFunc ¶
IsJudgeFunc checks if the input parameter is a function expected to return a boolean value.
func IsSequence ¶
IsSequence checks if the input parameter is array, slice or string.
func Len ¶
Len returns the length of the input parameter, it will return -1 if the input parameter is not a valid type.
Example ¶
// string fmt.Println(Len("hello")) // map fmt.Println(Len(map[string]int{"a": 1, "b": 2})) // slice fmt.Println(Len([]int{1, 2, 3}))
Output: 5 2 3
func ParseIndex ¶
ParseIndex parses the index to be within the range of the length.
func WillReHash ¶
WillReHash checks if the map will be rehashed based on the expected number of elements.
Types ¶
This section is empty.