slice

package
v2.18.5 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: AGPL-3.0 Imports: 1 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ascending

func Ascending[T constraints.Ordered](a, b T) int

func Contains

func Contains[T comparable](haystack []T, needle T) bool

func ContainsCompare

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

func Descending

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

func DifferenceFunc added in v2.15.0

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

func Find added in v2.17.0

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

Find returns the first element that satisfies the condition.

func New

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

New is a convenience method for creating []T.

func Omit added in v2.12.0

func Omit[T comparable](a []T, omits ...T) []T

Omit creates a new slice with the arguments omitted from the list.

func Overlap

func Overlap[T comparable](a []T, b []T) bool

Overlap returns if the 2 sets have any overlap (element(s) in common)

func OverlapCompare

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

func SameElements

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

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

func SymmetricDifference added in v2.15.0

func SymmetricDifference[T comparable](a, b []T) (add []T, remove []T)

SymmetricDifference returns the elements that need to be added and removed to get from set 'a' to set 'b'. Note that duplicates are ignored in sets. In classical set theory notation, SymmetricDifference returns all elements of {add} and {remove} together. It is more useful to return 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 main

import (
	"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]

func SymmetricDifferenceFunc added in v2.15.0

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

func ToStrings added in v2.13.0

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

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

func Unique

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

Unique returns a new slice with all duplicate elements removed.

func UniqueFunc added in v2.15.0

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

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL