Documentation
¶
Overview ¶
Package sliceutil provides functions for working with slices.
The "set" helpers are simple implementations, and don't operate on true "sets" (e.g. it will retain order, []int64 can contain duplicates). Consider using something like golang-set if you want to use sets and care a lot about performance: https://github.com/deckarep/golang-set
Index ¶
- func CSVtoInt64Slice(csv string) ([]int64, error)
- func Choose[T any](tt []T) T
- func ChooseString(l []string) stringdeprecated
- func Complement[T comparable](a, b []T) (aOnly, bOnly []T)
- func Contains[T comparable](tt []T, item T) bool
- func Difference[T comparable](set []T, others ...[]T) []T
- func Filter[T comparable](tt []T, fn func(T) bool) []T
- func FilterEmpty[T comparable](t T) bool
- func FilterInt(list []int64, fun func(int64) bool) []int64deprecated
- func FilterString(list []string, fun func(string) bool) []stringdeprecated
- func FilterStringEmpty(e string) booldeprecated
- func InFoldedStringSlice(list []string, str string) bool
- func InInt64Slice(list []int64, i int64) booldeprecated
- func InIntSlice(list []int, i int) booldeprecated
- func InStringSlice(list []string, str string) booldeprecated
- func InterfaceSliceTo(src []interface{}, dst interface{}) interface{}
- func Intersection[T comparable](a, b []T) []T
- func Join[T any](tt []T) string
- func JoinInt(ints []int64) stringdeprecated
- func JoinWith[T any](tt []T, delim string) string
- func Map[T comparable](tt []T, fn func(T) T) []T
- func MergeUnique[T comparable](tt [][]T) (result []T)
- func Range(start, end int) []int
- func Remove[T comparable](tt []T, remove T) (out []T)
- func RemoveString(list []string, s string) (out []string)deprecated
- func Repeat[T any](t T, n int) (tt []T)
- func RepeatString(s string, n int) (r []string)deprecated
- func StringMap(list []string, f func(string) string) []stringdeprecated
- func ToAnySlice[T any](tt []T) []any
- func UniqInt64(list []int64) []int64deprecated
- func UniqString(list []string) []stringdeprecated
- func Unique[T comparable](tt []T) []T
- func UniqueMergeSlices(s [][]int64) (result []int64)deprecated
- func Values[T comparable, N any](tt []T, fn func(T) N) []N
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CSVtoInt64Slice ¶
CSVtoInt64Slice converts a string of integers to a slice of int64.
func ChooseString
deprecated
added in
v2.2.1
func Complement ¶
func Complement[T comparable](a, b []T) (aOnly, bOnly []T)
Complement returns the complement of the two lists; that is, the first return value will contain elements that are only in "a" (and not in "b"), and the second return value will contain elements that are only in "b" (and not in "a").
func Contains ¶
func Contains[T comparable](tt []T, item T) bool
Contains returns true if item is in the provided slice.
func Difference ¶
func Difference[T comparable](set []T, others ...[]T) []T
Difference returns a new slice with elements that are in "set" but not in "others".
func Filter ¶
func Filter[T comparable](tt []T, fn func(T) bool) []T
Filter filters a list. The function will be called for every item and those that return false will not be included in the returned list.
func FilterEmpty ¶
func FilterEmpty[T comparable](t T) bool
FilterEmpty can be used as an argument for Filter() and will return false if e is zero value.
func FilterString
deprecated
added in
v2.2.1
func FilterStringEmpty
deprecated
added in
v2.2.1
func InFoldedStringSlice ¶
InFoldedStringSlice reports whether str is within list(case-insensitive)
func InInt64Slice
deprecated
added in
v2.2.1
func InIntSlice
deprecated
added in
v2.2.1
func InStringSlice
deprecated
added in
v2.2.1
func InterfaceSliceTo ¶
func InterfaceSliceTo(src []interface{}, dst interface{}) interface{}
InterfaceSliceTo converts []interface to any given slice. It will ~optimistically~ try to convert interface item to the dst item type
func Intersection ¶
func Intersection[T comparable](a, b []T) []T
Intersection returns the elements common to both a and b
func Join ¶
Join converts a slice of T to a comma separated string. Useful for inserting into a query without the option of parameterization.
func JoinWith ¶ added in v2.1.1
JoinWith converts a slice of T to a string using the provided delim.
func Map ¶
func Map[T comparable](tt []T, fn func(T) T) []T
Map returns a list where each item in list has been modified by fn
func MergeUnique ¶
func MergeUnique[T comparable](tt [][]T) (result []T)
MergeUnique takes a slice of slices and returns an unsorted slice of unique entries.
func Remove ¶
func Remove[T comparable](tt []T, remove T) (out []T)
Remove removes any occurrence of a string from a slice.
func RemoveString
deprecated
added in
v2.2.1
func RepeatString
deprecated
added in
v2.2.1
func ToAnySlice ¶ added in v2.2.3
ToAnySlice converts a slice of T to a slice of any.
func UniqString
deprecated
added in
v2.2.1
func Unique ¶
func Unique[T comparable](tt []T) []T
Unique removes duplicate entries from a list. The list does not have to be sorted.
func UniqueMergeSlices
deprecated
added in
v2.2.1
func Values ¶ added in v2.2.1
func Values[T comparable, N any](tt []T, fn func(T) N) []N
Values returns a list of items extracted from T, see test file for example
Types ¶
This section is empty.