Documentation
¶
Overview ¶
Package arrutil provides some util functions for array, slice
Index ¶
- Variables
- func AnyToSlice(sl any) (ls []any, err error)
- func AnyToString(arr any) string
- func AnyToStrings(arr any) []string
- func CloneSlice[T any](data []T) []T
- func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V
- func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
- func CombineToSMap(keys, values []string) map[string]string
- 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 Diff[T any](first, second []T, fn Comparer[T]) []T
- func Differences[T any](first, second []T, fn Comparer[T]) []T
- func ElemTypeEqualsComparer[T any](a, b T) int
- func ExceptWhile[T any](data []T, fn Predicate[T]) []T
- func Excepts[T any](first, second []T, fn Comparer[T]) []T
- func Filter[T any](ls []T, filter ...comdef.MatchFunc[T]) []T
- func Find[T any](source []T, fn Predicate[T]) (v T, err error)
- func FindOrDefault[T any](source []T, fn Predicate[T], defaultValue T) T
- 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[T ~string](elem T, ss []T) bool
- func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int
- func Int64sHas(ints []int64, val int64) bool
- func Intersects[T any](first, second []T, fn Comparer[T]) []T
- func IntsHas[T comdef.Integer](ints []T, val T) bool
- func IntsToString[T comdef.Integer](ints []T) string
- 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 JoinTyped[T any](sep string, arr ...T) string
- func Map[T any, V any](list []T, mapFn MapFn[T, V]) []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 ReflectEqualsComparer[T any](a, b T) int
- func Remove[T comdef.Compared](ls []T, val T) []T
- func Reverse[T any](ls []T)
- func SliceHas[T comdef.ScalarType](slice []T, val T) bool
- func SliceToInt64s(arr []any) []int64
- func SliceToString(arr ...any) string
- func SliceToStrings(arr []any) []string
- func StringEqualsComparer(a, b string) int
- func StringsAsInts(ss []string) []int
- func StringsContains(ss []string, s string) bool
- func StringsFilter(ss []string, filter ...comdef.StringMatchFunc) []string
- func StringsHas[T ~string](ss []T, val T) 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 StringsToAnys(ss []string) []any
- func StringsToInts(ss []string) (ints []int, err error)
- func StringsToSlice(ss []string) []any
- func StringsTryInts(ss []string) (ints []int, err error)
- func StringsUnique(ss []string) []string
- func TakeWhile[T any](data []T, fn Predicate[T]) []T
- func ToInt64s(arr any) (ret []int64, err error)
- func ToString[T any](arr []T) string
- func ToStrings(arr any) (ret []string, err error)
- func TrimStrings(ss []string, cutSet ...string) []string
- func TwowaySearch[T any](data []T, item T, fn Comparer[T]) (int, error)
- func Union[T any](first, second []T, fn Comparer[T]) []T
- func Unique[T ~string | comdef.XintOrFloat](list []T) []T
- func ValueEqualsComparer[T comdef.Compared](a, b T) int
- type ArrFormatter
- type Comparer
- type Ints
- type MapFn
- type Predicate
- type SortedList
- func (ls SortedList[T]) Contains(el T) bool
- func (ls SortedList[T]) Filter(filter ...comdef.MatchFunc[T]) SortedList[T]
- func (ls SortedList[T]) First(defVal ...T) T
- func (ls SortedList[T]) Has(el T) bool
- func (ls SortedList[T]) IsEmpty() bool
- func (ls SortedList[T]) Last(defVal ...T) T
- func (ls SortedList[T]) Len() int
- func (ls SortedList[T]) Less(i, j int) bool
- func (ls SortedList[T]) Remove(el T) SortedList[T]
- func (ls SortedList[T]) Sort()
- func (ls SortedList[T]) String() string
- func (ls SortedList[T]) Swap(i, j int)
- type Strings
Constants ¶
This section is empty.
Variables ¶
var ErrElementNotFound = errors.New("element not found")
ErrElementNotFound is the error returned when the element is not found.
var ErrInvalidType = errors.New("the input param type is invalid")
ErrInvalidType error
Functions ¶
func AnyToSlice ¶
AnyToSlice convert any(allow: array,slice) to []any
func AnyToString ¶
AnyToString simple and quickly convert any array, slice to string
func AnyToStrings ¶
AnyToStrings convert array or slice to []string
func CloneSlice ¶
func CloneSlice[T any](data []T) []T
CloneSlice Clone a slice.
data: the slice to clone. returns: the cloned slice.
func CombineToMap ¶
func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
CombineToMap combine []K and []V slice to map[K]V.
If keys length is greater than values, the extra keys will be ignored.
func CombineToSMap ¶
CombineToSMap combine two string-slice to map[string]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 Diff ¶
Diff Produces the set difference of two slice according to a comparer function. alias of Differences
func Differences ¶
Differences 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.
Example:
// Output: []string{"c"} Differences([]string{"a", "b", "c"}, []string{"a", "b"}, arrutil.StringEqualsComparer
func ElemTypeEqualsComparer ¶
ElemTypeEqualsComparer Comparer for struct/value. It will compare the struct by their element type.
returns: 0 if same type, -1 if not.
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.
Example:
// Output: []string{"a", "b"} sl := ExceptWhile([]string{"a", "b", "c"}, func(s string) bool { return s == "c" })
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.
Example:
// Output: []string{"c"} Excepts([]string{"a", "b", "c"}, []string{"a", "b"}, arrutil.StringEqualsComparer)
func Filter ¶
Filter given slice, default will filter zero value.
Usage:
// output: [a, b] ss := arrutil.Filter([]string{"a", "", "b", ""})
func Find ¶
Find Produces the 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.
Example:
// Output: "c" val := Find([]string{"a", "b", "c"}, func(s string) bool { return s == "c" })
func FindOrDefault ¶
FindOrDefault Produce the 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 value of the slice.
Example:
// Output: "d" val := FindOrDefault([]string{"a", "b", "c"}, func(s string) bool { return s == "d" }, "d")
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 IndexOf ¶
func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int
IndexOf value in given slice.
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.
Example:
// Output: []string{"a", "b"} Intersects([]string{"a", "b", "c"}, []string{"a", "b"}, arrutil.ValueEqualsComparer)
func IntsToString ¶
IntsToString convert []T to string
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 JoinTyped ¶
JoinTyped join typed []T slice to string.
Usage:
JoinTyped(",", 1,2,3) // "1,2,3" JoinTyped(",", "a","b","c") // "a,b,c" JoinTyped[any](",", "a",1,"c") // "a,1,c"
func Map ¶
Map a list to new list
eg: mapping [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 QuietStrings ¶
QuietStrings safe convert []any to []string
func ReflectEqualsComparer ¶
ReflectEqualsComparer Comparer for struct ptr. It will compare by reflect.Value
returns: 0 if equal, -1 if a != b
func Remove ¶
Remove give element from slice []T.
eg: []string{"site", "user", "info", "0"} -> []string{"site", "user", "info"}
func Reverse ¶
func Reverse[T any](ls []T)
Reverse any T slice.
eg: []string{"site", "user", "info", "0"} -> []string{"0", "info", "user", "site"}
func SliceHas ¶
func SliceHas[T comdef.ScalarType](slice []T, val T) bool
SliceHas check the slice contains the given value
func SliceToStrings ¶
SliceToStrings safe convert []any to []string
func StringEqualsComparer ¶
StringEqualsComparer Comparer for string. It will compare the string by their value.
returns: 0 if equal, -1 if a != b
func StringsContains ¶
StringsContains check string slice contains string
func StringsFilter ¶
func StringsFilter(ss []string, filter ...comdef.StringMatchFunc) []string
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 value form a string slice
func StringsToInts ¶
StringsToInts string slice to int slice
func StringsToSlice ¶
StringsToSlice convert []string to []any. alias of StringsToAnys()
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.
Example:
// Output: []string{"a", "b"} sl := TakeWhile([]string{"a", "b", "c"}, func(s string) bool { return s != "c" })
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.
Example:
// Output: []string{"a", "b", "c"} sl := Union([]string{"a", "b", "c"}, []string{"a", "b"}, arrutil.ValueEqualsComparer)
func Unique ¶
func Unique[T ~string | comdef.XintOrFloat](list []T) []T
Unique value in the given slice data.
func ValueEqualsComparer ¶
ValueEqualsComparer Comparer for comdef.Compared type. It will compare by their value.
returns: 0 if equal, -1 if a != b
Types ¶
type ArrFormatter ¶
type ArrFormatter struct { comdef.BaseFormatter // Prefix string for each element Prefix string // Indent string for format each element Indent string // ClosePrefix on before end char: ] 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 SortedList ¶
SortedList definition for compared type
func (SortedList[T]) Filter ¶
func (ls SortedList[T]) Filter(filter ...comdef.MatchFunc[T]) SortedList[T]
Filter the slice, default will filter zero value.
func (SortedList[T]) Remove ¶
func (ls SortedList[T]) Remove(el T) SortedList[T]
Remove given element