Documentation ¶
Overview ¶
Package arrutil provides some util functions for array, slice
Index ¶
- Constants
- Variables
- func AnyToString(arr any) string
- func AnyToStrings(arr any) []string
- func CloneSlice(data any) any
- func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V
- func Contains(arr, val any) bool
- func ContainsAll[T comdef.ScalarType](list, values []T) bool
- func ConvType[T any, R any](arr []T, newElemTyp R) ([]R, error)
- func ExceptWhile(data any, fn Predicate) any
- func Excepts(first, second any, fn Comparer) any
- func Find(source any, fn Predicate) (any, error)
- func FindOrDefault(source any, fn Predicate, defaultValue any) any
- func FormatIndent(arr any, indent string) string
- func GetRandomOne[T any](arr []T) T
- func HasValue(arr, val any) bool
- func In[T comdef.ScalarType](value T, list []T) bool
- func InStrings(elem string, ss []string) bool
- func Int64sHas(ints []int64, val int64) bool
- func Intersects(first any, second any, fn Comparer) any
- func IntsHas(ints []int, val int) bool
- func IsParent[T comdef.ScalarType](values, list []T) bool
- func IsSubList[T comdef.ScalarType](values, list []T) bool
- func JoinSlice(sep string, arr ...any) string
- func JoinStrings(sep string, ss ...string) string
- func MakeEmptySlice(itemType reflect.Type) any
- func Map[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V
- func MustToInt64s(arr any) []int64
- func MustToStrings(arr any) []string
- func NotContains(arr, val any) bool
- func NotIn[T comdef.ScalarType](value T, list []T) bool
- func QuietStrings(arr []any) []string
- func RandomOne[T any](arr []T) T
- func Reverse(ss []string)
- func SliceToInt64s(arr []any) []int64
- func SliceToString(arr ...any) string
- func SliceToStrings(arr []any) []string
- func StringsAsInts(ss []string) []int
- func StringsFilter(ss []string, filter ...func(s string) bool) []string
- func StringsHas(ss []string, val string) bool
- func StringsJoin(sep string, ss ...string) string
- func StringsMap(ss []string, mapFn func(s string) string) []string
- func StringsRemove(ss []string, s string) []string
- func StringsToInts(ss []string) (ints []int, err error)
- func StringsToSlice(ss []string) []any
- func StringsTryInts(ss []string) (ints []int, err error)
- func TakeWhile(data any, fn Predicate) any
- func ToInt64s(arr any) (ret []int64, err error)
- func ToString(arr []any) string
- func ToStrings(arr any) (ret []string, err error)
- func TrimStrings(ss []string, cutSet ...string) []string
- func TwowaySearch(data any, item any, fn Comparer) (int, error)
- func Union(first, second any, fn Comparer) any
- func Unique[T ~string | comdef.XintOrFloat](arr []T) []T
- type ArrFormatter
- type Comparer
- type Ints
- type Predicate
- type Strings
Constants ¶
const ErrElementNotFound = "element not found"
ErrElementNotFound is the error returned when the element is not found.
Variables ¶
var ErrInvalidType = errors.New("the input param type is invalid")
ErrInvalidType error
Functions ¶
func AnyToString ¶
AnyToString simple and quickly convert any array, slice to string
func AnyToStrings ¶
AnyToStrings convert array or slice to []string
func Contains ¶
Contains check slice/array(strings, intXs, uintXs) should be contained the given value(int(X),string).
TIP: Difference the In(), Contains() will try to convert value type, and Contains() support array type.
func ContainsAll ¶
func ContainsAll[T comdef.ScalarType](list, values []T) bool
ContainsAll check given values is sub-list of sample list.
func ConvType ¶
ConvType convert type of slice elements to new type slice, by the given newElemTyp type.
Supports conversion between []string, []intX, []uintX, []floatX.
Usage:
ints, _ := arrutil.ConvType([]string{"12", "23"}, 1) // []int{12, 23}
func ExceptWhile ¶
ExceptWhile Produce the set of a slice except with a predicate function, Produce original slice when predicate function not match.
data: the slice. MUST BE A SLICE. fn: the predicate function. returns: the set of the slice.
func Excepts ¶
Excepts Produces the set difference of two slice according to a comparer function.
first: the first slice. MUST BE A SLICE. second: the second slice. MUST BE A SLICE. fn: the comparer function. returns: the difference of the two slices.
func Find ¶
Find Produces the struct/value of a slice according to a predicate function.
source: the slice. MUST BE A SLICE. fn: the predicate function. returns: the struct/value of the slice.
func FindOrDefault ¶
FindOrDefault Produce the struct/value f a slice to a predicate function, Produce default value when predicate function not found.
source: the slice. MUST BE A SLICE. fn: the predicate function. defaultValue: the default value. returns: the struct/value of the slice.
func FormatIndent ¶
FormatIndent array data to string.
func GetRandomOne ¶
func GetRandomOne[T any](arr []T) T
GetRandomOne get random element from an array/slice
func HasValue ¶
HasValue check array(strings, intXs, uintXs) should be contained the given value(int(X),string).
func In ¶
func In[T comdef.ScalarType](value T, list []T) bool
In check the given value whether in the list
func Intersects ¶
Intersects Produces to intersect of two slice according to a comparer function.
first: the first slice. MUST BE A SLICE. second: the second slice. MUST BE A SLICE. fn: the comparer function. returns: to intersect of the two slices.
func IsParent ¶
func IsParent[T comdef.ScalarType](values, list []T) bool
IsParent check given values is parent-list of samples.
func IsSubList ¶
func IsSubList[T comdef.ScalarType](values, list []T) bool
IsSubList check given values is sub-list of sample list.
func JoinStrings ¶
JoinStrings alias of strings.Join
func MakeEmptySlice ¶
MakeEmptySlice Create a new slice with the elements of the source that satisfy the predicate.
itemType: the type of the elements in the source. returns: the new slice.
func Map ¶
Map an object list [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...]
func MustToInt64s ¶
MustToInt64s convert any(allow: array,slice) to []int64
func MustToStrings ¶
MustToStrings convert array or slice to []string
func NotContains ¶
NotContains check array(strings, ints, uints) should be not contains the given value.
func NotIn ¶
func NotIn[T comdef.ScalarType](value T, list []T) bool
NotIn check the given value whether not in the list
func Reverse ¶
func Reverse(ss []string)
Reverse string slice [site user info 0] -> [0 info user site]
func SliceToStrings ¶
SliceToStrings convert []any to []string
func StringsFilter ¶
StringsFilter given strings, default will filter emtpy string.
Usage:
// output: [a, b] ss := arrutil.StringsFilter([]string{"a", "", "b", ""})
func StringsHas ¶
StringsHas check the []string contains the given element
func StringsJoin ¶
StringsJoin alias of strings.Join
func StringsMap ¶
StringsMap handle each string item, map to new strings
func StringsRemove ¶
StringsRemove a value form a string slice
func StringsToInts ¶
StringsToInts string slice to int slice
func StringsToSlice ¶
StringsToSlice convert []string to []any
func StringsTryInts ¶
StringsTryInts string slice to int slice
func TakeWhile ¶
TakeWhile Produce the set of a slice according to a predicate function, Produce empty slice when predicate function not matched.
data: the slice. MUST BE A SLICE. fn: the predicate function. returns: the set of the slice.
func TrimStrings ¶
TrimStrings trim string slice item.
Usage:
// output: [a, b, c] ss := arrutil.TrimStrings([]string{",a", "b.", ",.c,"}, ",.")
func TwowaySearch ¶
TwowaySearch Find specialized element in a slice forward and backward in the same time, should be more quickly.
data: the slice to search in. MUST BE A SLICE. item: the element to search. fn: the comparer function. return: the index of the element, or -1 if not found.
func Union ¶
Union Produces the set union of two slice according to a comparer function
first: the first slice. MUST BE A SLICE. second: the second slice. MUST BE A SLICE. fn: the comparer function. returns: the union of the two slices.
func Unique ¶
func Unique[T ~string | comdef.XintOrFloat](arr []T) []T
Unique value in the given slice data.
Types ¶
type ArrFormatter ¶
type ArrFormatter struct { comdef.BaseFormatter // Prefix string for each element Prefix string // Indent string for format each element Indent string // ClosePrefix string for last "]" ClosePrefix string }
ArrFormatter struct
func (*ArrFormatter) FormatTo ¶
func (f *ArrFormatter) FormatTo(w io.Writer)
FormatTo to custom buffer
func (*ArrFormatter) WithFn ¶
func (f *ArrFormatter) WithFn(fn func(f *ArrFormatter)) *ArrFormatter
WithFn for config self
func (*ArrFormatter) WithIndent ¶
func (f *ArrFormatter) WithIndent(indent string) *ArrFormatter
WithIndent string
type Comparer ¶
Comparer Function to compare two elements.
var ( // StringEqualsComparer Comparer for string. It will compare the string by their value. // returns: 0 if equal, -1 if a != b StringEqualsComparer Comparer = func(a, b any) int { typeOfA := reflect.TypeOf(a) if typeOfA.Kind() == reflect.Ptr { typeOfA = typeOfA.Elem() } typeOfB := reflect.TypeOf(b) if typeOfB.Kind() == reflect.Ptr { typeOfB = typeOfB.Elem() } if typeOfA != typeOfB { return -1 } strA := "" strB := "" if val, ok := a.(string); ok { strA = val } else if val, ok := a.(*string); ok { strA = *val } else { return -1 } if val, ok := b.(string); ok { strB = val } else if val, ok := b.(*string); ok { strB = *val } else { return -1 } if strA == strB { return 0 } return -1 } // ReferenceEqualsComparer Comparer for strcut ptr. It will compare the struct by their ptr addr. // returns: 0 if equal, -1 if a != b ReferenceEqualsComparer Comparer = func(a, b any) int { if a == b { return 0 } return -1 } // ElemTypeEqualsComparer Comparer for struct/value. It will compare the struct by their element type (reflect.Type.Elem()). // returns: 0 if same type, -1 if not. ElemTypeEqualsComparer Comparer = func(a, b any) int { at := reflect.TypeOf(a) bt := reflect.TypeOf(b) if at.Kind() == reflect.Ptr { at = at.Elem() } if bt.Kind() == reflect.Ptr { bt = bt.Elem() } if at == bt { return 0 } return -1 } )