github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/conv.go (about) 1 package easy 2 3 import ( 4 "strconv" 5 6 "github.com/jxskiss/gopkg/v2/internal/constraints" 7 ) 8 9 // ConvInts converts slice of integers of type T1 to a new 10 // slice of integers of type T2. 11 // The input data must be convertable to T2. 12 func ConvInts[T1, T2 constraints.Integer](slice []T1) []T2 { 13 if len(slice) == 0 { 14 return nil 15 } 16 out := make([]T2, len(slice)) 17 for i, x := range slice { 18 out[i] = T2(x) 19 } 20 return out 21 } 22 23 // FormatInts converts slice of integers to a slice of strings. 24 // It returns nil if there is no element in given slice. 25 func FormatInts[T constraints.Integer](slice []T, base int) []string { 26 if len(slice) == 0 { 27 return nil 28 } 29 out := make([]string, len(slice)) 30 for i, x := range slice { 31 out[i] = strconv.FormatInt(int64(x), base) 32 } 33 return out 34 } 35 36 // ParseInts converts slice of strings to a slice of integers. 37 // It returns nil if there is no element in given slice. 38 // 39 // Note that if the input data contains non-integer strings, the 40 // errors returned from strconv.ParseInt are ignored, and the 41 // returned slice will have less elements than the input slice. 42 func ParseInts[T constraints.Integer](slice []string, base int) []T { 43 if len(slice) == 0 { 44 return nil 45 } 46 out := make([]T, 0, len(slice)) 47 for _, x := range slice { 48 iv, err := strconv.ParseInt(x, base, 0) 49 if err == nil { 50 out = append(out, T(iv)) 51 } 52 } 53 return out 54 } 55 56 // ToBoolMap converts the given slice to a hash set, 57 // using elements from the slice as keys and true as values. 58 func ToBoolMap[S ~[]E, E comparable](slice S) map[E]bool { 59 if len(slice) == 0 { 60 return nil 61 } 62 out := make(map[E]bool, len(slice)) 63 for _, elem := range slice { 64 out[elem] = true 65 } 66 return out 67 } 68 69 // ToMap converts the given slice to a map, it calls f for each element 70 // in slice to get key values to construct the returned map. 71 func ToMap[S ~[]E, E any, K comparable, V any](slice S, f func(E) (K, V)) map[K]V { 72 if len(slice) == 0 { 73 return nil 74 } 75 out := make(map[K]V, len(slice)) 76 for _, elem := range slice { 77 k, v := f(elem) 78 out[k] = v 79 } 80 return out 81 } 82 83 // ToInterfaceSlice returns a []interface{} containing elements from slice. 84 // 85 // Deprecated: this function has been renamed to ToAnySlice. 86 func ToInterfaceSlice[S ~[]E, E any](slice S) []any { 87 return ToAnySlice(slice) 88 } 89 90 // ToAnySlice returns a []any containing elements from slice. 91 func ToAnySlice[S ~[]E, E any](slice S) []any { 92 if len(slice) == 0 { 93 return nil 94 } 95 out := make([]any, len(slice)) 96 for i, elem := range slice { 97 out[i] = elem 98 } 99 return out 100 } 101 102 // ToTypedSlice returns a []T slice containing elements from slice. 103 func ToTypedSlice[T any](slice []any) []T { 104 if len(slice) == 0 { 105 return nil 106 } 107 out := make([]T, len(slice)) 108 for i, elem := range slice { 109 out[i] = elem.(T) 110 } 111 return out 112 }