slice
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Index¶
- func Ascending[T constraints.Ordered](a, b T) int
- func Contains[T comparable](haystack []T, needle T) bool
- func ContainsCompare[T any](haystack []T, needle T, equal func(a, b T) bool) bool
- func Convert[F any, T any](a []F, f func(F) T) []T
- func CountConsecutive[T comparable](needle T, haystack ...T) int
- func CountMatchingPairs[A, B any](a []A, b []B, match func(A, B) bool) int
- func Descending[T constraints.Ordered](a, b T) int
- func DifferenceFunc[T any](a []T, b []T, equal func(a, b T) bool) []T
- func Filter[T any](haystack []T, cond func(T) bool) []T
- func Find[T any](haystack []T, cond func(T) bool) (T, bool)
- func New[T any](items ...T) []T
- func Omit[T comparable](a []T, omits ...T) []T
- func Overlap[T comparable](a []T, b []T) bool
- func OverlapCompare[T any](a []T, b []T, equal func(a, b T) bool) bool
- func SameElements[T comparable](a []T, b []T) bool
- func StringEnums[E ~string](a []string) []E
- func SymmetricDifference[T comparable](a, b []T) (add []T, remove []T)
- func SymmetricDifferenceFunc[T any](a, b []T, equal func(a, b T) bool) (add []T, remove []T)
- func ToMapFunc[T any, K comparable, V any](a []T, cnv func(t T) (K, V)) map[K]V
- func ToStrings[T ~string](a []T) []string
- func Unique[T comparable](a []T) []T
- func UniqueFunc[T any](a []T, equal func(a, b T) bool) []T
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcAscending¶
func Ascending[Tconstraints.Ordered](a, b T)int
funcContains¶
func Contains[Tcomparable](haystack []T, needle T)bool
funcContainsCompare¶
funcConvert¶added inv2.24.0
Convert converts a slice of type F to a slice of type T using the provided function f.
funcCountConsecutive¶added inv2.20.0
func CountConsecutive[Tcomparable](needle T, haystack ...T)int
funcCountMatchingPairs¶added inv2.22.0
funcDescending¶
func Descending[Tconstraints.Ordered](a, b T)int
funcDifferenceFunc¶added inv2.15.0
funcOmit¶added inv2.12.0
func Omit[Tcomparable](a []T, omits ...T) []T
Omit creates a new slice with the arguments omitted from the list.
funcOverlap¶
func Overlap[Tcomparable](a []T, b []T)bool
Overlap returns if the 2 sets have any overlap (element(s) in common)
funcOverlapCompare¶
funcSameElements¶
func SameElements[Tcomparable](a []T, b []T)bool
SameElements returns true if the 2 lists have the same elements in anyorder.
funcStringEnums¶added inv2.19.0
funcSymmetricDifference¶added inv2.15.0
func SymmetricDifference[Tcomparable](a, b []T) (add []T, remove []T)
SymmetricDifference returns the elements that need to be added and removedto get from set 'a' to set 'b'. Note that duplicates are ignored in sets.In classical set theory notation, SymmetricDifference returnsall elements of {add} and {remove} together. It is more useful toreturn them as their own slices.Notation: A Δ B = (A\B) ∪ (B\A)Example:
a := []int{1, 3, 4}b := []int{1, 2, 2, 2}add, remove := SymmetricDifference(a, b)fmt.Println(add) // [2]fmt.Println(remove) // [3, 4]
Example¶
package mainimport ("fmt""github.com/coder/coder/v2/coderd/util/slice")func main() {// The goal of this function is to find the elements to add & remove from// set 'a' to make it equal to set 'b'.a := []int{1, 2, 5, 6, 6, 6}b := []int{2, 3, 3, 3, 4, 5}add, remove := slice.SymmetricDifference(a, b)fmt.Println("Elements to add:", add)fmt.Println("Elements to remove:", remove)}
Output:Elements to add: [3 4]Elements to remove: [1 6]
funcSymmetricDifferenceFunc¶added inv2.15.0
funcToMapFunc¶added inv2.24.0
func ToMapFunc[Tany, Kcomparable, Vany](a []T, cnv func(t T) (K, V)) map[K]V
funcUnique¶
func Unique[Tcomparable](a []T) []T
Unique returns a new slice with all duplicate elements removed.
funcUniqueFunc¶added inv2.15.0
Types¶
This section is empty.