-
Notifications
You must be signed in to change notification settings - Fork 0
/
to.go
118 lines (100 loc) · 3.59 KB
/
to.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package slice
import (
"fmt"
"strings"
)
// ---------------------------------------------------------------------------------------------------------------------
// To 类似Map,不同的是更推荐这个方法只用来做做类型转换
func To[From, To any](slice []From, itemMapFunc func(index int, value From) To) []To {
return Map[From, To](slice, itemMapFunc)
}
// ---------------------------------------------------------------------------------------------------------------------
// ToStringSlice 把切片转为string类型的切片
func ToStringSlice[T any](slice []T) []string {
return To(slice, func(index int, item T) string {
return fmt.Sprintf("%#v", item)
})
}
// ToStringSliceByFunc 使用自定义的函数把切片转为string类型的切片
func ToStringSliceByFunc[T any](slice []T, itemMapFunc func(index int, value T) string) []string {
stringSlice := make([]string, 0)
for index, item := range slice {
s := itemMapFunc(index, item)
stringSlice = append(stringSlice, s)
}
return stringSlice
}
// ToString 把整个切片转为一个字符串,通常用于打印、记录之类的
func ToString[T any](slice []T) string {
sb := strings.Builder{}
for _, item := range slice {
sb.WriteString(fmt.Sprintf("%#v", item))
}
return sb.String()
}
// ---------------------------------------------------------------------------------------------------------------------
// ToMap 把切片转为map,将索引作为map的key,将对应下标元素作为map的value
func ToMap[T, K int](slice []T) map[int]T {
resultMap := make(map[int]T, 0)
for index, item := range slice {
resultMap[index] = item
}
return resultMap
}
// ToMapByFunc 把切片转为map,使用自定义的函数从元素中生成key和value
func ToMapByFunc[T any, K comparable, V any](slice []T, toMapFunc func(index int, item T) (K, V)) map[K]V {
resultMap := make(map[K]V, 0)
for index, item := range slice {
k, v := toMapFunc(index, item)
resultMap[k] = v
}
return resultMap
}
// ---------------------------------------------------------------------------------------------------------------------
// ToSet 把切片转为set
func ToSet[T comparable](slice []T) map[T]struct{} {
set := make(map[T]struct{}, 0)
for _, item := range slice {
set[item] = struct{}{}
}
return set
}
// ToSetByFunc 把切片转为set,根据自定义的函数从切片的元素生成set的key
func ToSetByFunc[T any, K comparable](slice []T, keyFunc func(index int, item T) K) map[K]struct{} {
set := make(map[K]struct{}, 0)
for index, item := range slice {
key := keyFunc(index, item)
set[key] = struct{}{}
}
return set
}
// ---------------------------------------------------------------------------------------------------------------------
// Flat2 二维切片打平为一维切片
func Flat2[T any](slice [][]T) []T {
resultSlice := make([]T, 0)
for _, itemSlice := range slice {
for _, item := range itemSlice {
resultSlice = append(resultSlice, item)
}
}
return resultSlice
}
// ---------------------------------------------------------------------------------------------------------------------
// ToMatrix 一维切片转换为矩阵
func ToMatrix[T any](slice []T, columnCount int) [][]T {
matrix := make([][]T, 0)
consumer := NewSliceConsumer(slice)
for !consumer.IsDone() {
row := make([]T, columnCount, columnCount)
matrix = append(matrix, row)
for index := range row {
e, err := consumer.TakeE()
if err != nil {
return matrix
}
row[index] = e
}
}
return matrix
}
// ------------------------------------------------ ---------------------------------------------------------------------