1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package slice 18 19 import ( 20 "sort" 21 ) 22 23 // SortInts64 sorts []int64 in increasing order 24 func SortInts64(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) } 25 26 // ContainsString checks if a given slice of strings contains the provided string. 27 // If a modifier func is provided, it is called with the slice item before the comparation. 28 func ContainsString(slice []string, s string, modifier func(s string) string) bool { 29 for _, item := range slice { 30 if item == s { 31 return true 32 } 33 if modifier != nil && modifier(item) == s { 34 return true 35 } 36 } 37 return false 38 } 39 40 // ToSet returns a single slice containing the unique values from one or more slices. The order of the items in the 41 // result is not guaranteed. 42 func ToSet[T comparable](slices ...[]T) []T { 43 if len(slices) == 0 { 44 return nil 45 } 46 m := map[T]struct{}{} 47 for _, slice := range slices { 48 for _, value := range slice { 49 m[value] = struct{}{} 50 } 51 } 52 result := []T{} 53 for k := range m { 54 result = append(result, k) 55 } 56 return result 57 } 58