github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/slice/contains.go (about)

     1  package slice
     2  
     3  // AppendUnique appends all elements that are not already present in dest to dest
     4  // and returns the result.
     5  func AppendUnique[E comparable](dest []E, src ...E) []E {
     6  	for _, v := range src {
     7  		if !Contains(dest, v) {
     8  			dest = append(dest, v)
     9  		}
    10  	}
    11  	return dest
    12  }
    13  
    14  // Contains returns true if the given slice contains the given element.
    15  func Contains[E comparable](vs []E, e E) bool {
    16  	for _, v := range vs {
    17  		if e == v {
    18  			return true
    19  		}
    20  	}
    21  	return false
    22  }
    23  
    24  // ContainsAll returns true if the first slice contains all elements in the second slice.
    25  func ContainsAll[E comparable](vs []E, es []E) bool {
    26  	for _, e := range es {
    27  		if !Contains(vs, e) {
    28  			return false
    29  		}
    30  	}
    31  	return true
    32  }
    33  
    34  // ContainsAny returns true if the first slice contains at least one of the elements in the second slice.
    35  func ContainsAny[E comparable](vs []E, es []E) bool {
    36  	for _, e := range es {
    37  		if Contains(vs, e) {
    38  			return true
    39  		}
    40  	}
    41  	return false
    42  }