common

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package common contains common constants, functions and errors used throughout the flex codebase.

Index

Examples

Constants

This section is empty.

Variables

ArrayType incluedes array and slice.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
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.

View Source
var ErrNotIterable = errors.New("the input parameter is not iterable")

ErrNotIterable is an error indicating that the input parameter is not iterable as expected.

View Source
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.

View Source
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.

View Source
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.

View Source
var ErrOutOfRange = errors.New("the index is out of range")

ErrOutOfRange is an error indicating that the index is out of range.

View Source
var ErrTooManyArguments = errors.New("too many arguments")

ErrTooManyArguments is an error indicating that there are more arguments than expected.

View Source
var ErrUnexpectedParamCount = errors.New("unexpected number of input parameters")

ErrUnexpectedParamCount is an error indicating that the number of input parameters is unexpected.

View Source
var ErrUnexpectedReturnCount = errors.New("unexpected number of return values")

ErrUnexpectedReturnCount is an error indicating that the number of return values is unexpected.

View Source
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.

View Source
var IterableContainers = append(SequenceType, reflect.Map)

IterableContainers includes array, slice, string and map.

View Source
var SequenceType = append(ArrayType, reflect.String)

SequenceType includes array, slice and string.

Functions

func CheckRange

func CheckRange(start, end, step, length int) (err error)

CheckRange checks if the range is valid and within the range of the length.

func Contains

func Contains(entry, value any) bool

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

func ConvertMapToLists(entry map[any]any) (keys, values []any, length int)

ConvertMapToLists converts a map to a key list and a value list, and returns the length of the map.

func ConvertStringToList

func ConvertStringToList(entry string) (output []any)

ConvertStringToList converts a string to a list of characters.

func CopyList

func CopyList(entry reflect.Value, length int) (output []any)

CopyList copies the list to a new list with the given length.

func CopyMap

func CopyMap(entry reflect.Value, capacity int) (output map[any]any)

CopyMap copies the map to a new map with the given capcacity.

func Count

func Count(entry, value any) (count int)

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

func Equal(a, b any) (equal bool)

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

func GetMapInitialCapacity(elementCount int) int

GetMapInitialCapacity calculates the initial capacity of a map based on the expected number of elements.

func IsInputFuncValid

func IsInputFuncValid(f any, inputCount, outputCount int) error

IsInputFuncValid checks if the input parameter is a valid function and if it has the expected number of input and output parameters.

func IsJudgeFunc

func IsJudgeFunc(f any) (err error)

IsJudgeFunc checks if the input parameter is a function expected to return a boolean value.

func IsList

func IsList(entry any) error

IsList checks if the input parameter is array or slice.

func IsSequence

func IsSequence(entry any) error

IsSequence checks if the input parameter is array, slice or string.

func Len

func Len(entry any) (length int)

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

func ParseIndex(index, length int) int

ParseIndex parses the index to be within the range of the length.

func WillReHash

func WillReHash(oldElementCount, newElementCount int) bool

WillReHash checks if the map will be rehashed based on the expected number of elements.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL