Movatterモバイル変換


[0]ホーム

URL:


slice

package
v2.24.2Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2025 License:AGPL-3.0Imports:1Imported by:1

Details

Repository

github.com/coder/coder

Links

Documentation

Index

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

func ContainsCompare[Tany](haystack []T, needle T, equal func(a, b T)bool)bool

funcConvertadded inv2.24.0

func Convert[Fany, Tany](a []F, f func(F) T) []T

Convert converts a slice of type F to a slice of type T using the provided function f.

funcCountConsecutiveadded inv2.20.0

func CountConsecutive[Tcomparable](needle T, haystack ...T)int

funcCountMatchingPairsadded inv2.22.0

func CountMatchingPairs[A, Bany](a []A, b []B, match func(A, B)bool)int

funcDescending

func Descending[Tconstraints.Ordered](a, b T)int

funcDifferenceFuncadded inv2.15.0

func DifferenceFunc[Tany](a []T, b []T, equal func(a, b T)bool) []T

funcFilteradded inv2.22.0

func Filter[Tany](haystack []T, cond func(T)bool) []T

Filter returns all elements that satisfy the condition.

funcFindadded inv2.17.0

func Find[Tany](haystack []T, cond func(T)bool) (T,bool)

Find returns the first element that satisfies the condition.

funcNew

func New[Tany](items ...T) []T

New is a convenience method for creating []T.

funcOmitadded 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

func OverlapCompare[Tany](a []T, b []T, equal func(a, b T)bool)bool

funcSameElements

func SameElements[Tcomparable](a []T, b []T)bool

SameElements returns true if the 2 lists have the same elements in anyorder.

funcStringEnumsadded inv2.19.0

func StringEnums[E ~string](a []string) []E

funcSymmetricDifferenceadded 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]

funcSymmetricDifferenceFuncadded inv2.15.0

func SymmetricDifferenceFunc[Tany](a, b []T, equal func(a, b T)bool) (add []T, remove []T)

funcToMapFuncadded inv2.24.0

func ToMapFunc[Tany, Kcomparable, Vany](a []T, cnv func(t T) (K, V)) map[K]V

funcToStringsadded inv2.13.0

func ToStrings[T ~string](a []T) []string

ToStrings works for any type where the base type is a string.

funcUnique

func Unique[Tcomparable](a []T) []T

Unique returns a new slice with all duplicate elements removed.

funcUniqueFuncadded inv2.15.0

func UniqueFunc[Tany](a []T, equal func(a, b T)bool) []T

Types

This section is empty.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp