forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
334 lines (283 loc) · 5.68 KB
/
base.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package utils
import (
"fmt"
"github.com/goccy/go-json"
"math/rand"
"time"
)
func Intn(n int) int {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
return r.Intn(n)
}
func Intn64(n int64) int64 {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
return r.Int63n(n)
}
func IntnSeed(n int, seed int) int {
// unix nano is the same if called in the same nanosecond, so we need to add another random seed
source := rand.NewSource(time.Now().UnixNano() + int64(seed))
r := rand.New(source)
return r.Intn(n)
}
func IntnSeq(n int, len int) (res []int) {
for i := 0; i < len; i++ {
res = append(res, IntnSeed(n, i))
}
return res
}
func Sum[T int | int64 | float32 | float64](arr []T) T {
var res T
for _, v := range arr {
res += v
}
return res
}
func Contains[T comparable](value T, slice []T) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}
func ToPtr[T any](value T) *T {
return &value
}
func TryGet[T any](arr []T, index int) T {
if index >= len(arr) {
return arr[0]
}
return arr[index]
}
func Debug[T any](v T) T {
fmt.Println(v)
return v
}
func Insert[T any](arr []T, index int, value T) []T {
arr = append(arr, value)
copy(arr[index+1:], arr[index:])
arr[index] = value
return arr
}
func InsertSlice[T any](arr []T, index int, value []T) []T {
arr = append(arr, value...)
copy(arr[index+len(value):], arr[index:])
copy(arr[index:], value)
return arr
}
func Collect[T any](arr ...[]T) []T {
res := make([]T, 0)
for _, v := range arr {
res = append(res, v...)
}
return res
}
func Append[T any](arr []T, value T) []T {
return append(arr, value)
}
func AppendSlice[T any](arr []T, value []T) []T {
return append(arr, value...)
}
func Prepend[T any](arr []T, value T) []T {
return append([]T{value}, arr...)
}
func PrependSlice[T any](arr []T, value []T) []T {
return append(value, arr...)
}
func Remove[T any](arr []T, index int) []T {
return append(arr[:index], arr[index+1:]...)
}
func RemoveSlice[T any](arr []T, index int, length int) []T {
return append(arr[:index], arr[index+length:]...)
}
func ToJson(value interface{}) string {
if res, err := json.Marshal(value); err == nil {
return string(res)
} else {
return "{}"
}
}
func UnmarshalJson[T any](value string) T {
var res T
if err := json.Unmarshal([]byte(value), &res); err == nil {
return res
} else {
return res
}
}
func DeepCopy[T any](value T) T {
return UnmarshalJson[T](ToJson(value))
}
func GetSegment[T any](arr []T, length int) []T {
if length > len(arr) {
return arr
}
return arr[:length]
}
func GetSegmentString(arr string, length int) string {
if length > len(arr) {
return arr
}
return arr[:length]
}
func GetLatestSegment[T any](arr []T, length int) []T {
if length > len(arr) {
return arr
}
return arr[len(arr)-length:]
}
func Reverse[T any](arr []T) []T {
for i := 0; i < len(arr)/2; i++ {
arr[i], arr[len(arr)-i-1] = arr[len(arr)-i-1], arr[i]
}
return arr
}
func Multi[T comparable](condition bool, tval, fval T) T {
if condition {
return tval
} else {
return fval
}
}
func MultiF[T comparable](condition bool, tval func() T, fval T) T {
if condition {
return tval()
} else {
return fval
}
}
func InsertChannel[T any](ch chan T, value T, index int) {
var arr []T
for i := 0; i < len(ch); i++ {
arr = append(arr, <-ch)
}
arr = Insert(arr, index, value)
for _, v := range arr {
ch <- v
}
}
func Sort[T any](arr []T, compare func(a, b T) bool) []T {
if len(arr) <= 1 {
return arr
}
var result []T
var temp []T
var hasFirst bool
var first T
for _, item := range arr {
if !hasFirst {
first = item
hasFirst = true
continue
}
if compare(item, first) {
temp = append(temp, item)
} else {
result = append(result, first)
result = append(result, Sort(temp, compare)...)
first = item
temp = []T{}
}
}
if len(temp) > 0 {
result = append(result, first)
result = append(result, Sort(temp, compare)...)
} else if hasFirst {
result = append(result, first)
}
return result
}
func Each[T any, U any](arr []T, f func(T) U) []U {
var res []U
for _, v := range arr {
res = append(res, f(v))
}
return res
}
func EachObject[T any, V any](arr []T, f func(T) (string, V)) map[string]V {
res := make(map[string]V)
for _, v := range arr {
key, val := f(v)
res[key] = val
}
return res
}
func EachNotNil[T any, U any](arr []T, f func(T) *U) []U {
var res []U
for _, v := range arr {
if val := f(v); val != nil {
res = append(res, *val)
}
}
return res
}
func Filter[T any](arr []T, f func(T) bool) []T {
var res []T
for _, v := range arr {
if f(v) {
res = append(res, v)
}
}
return res
}
func Sleep(ms int) {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
func GetPtrVal[T any](ptr *T, def T) T {
if ptr == nil {
return def
}
return *ptr
}
func LimitMax[T int | int64 | float32 | float64](value T, max T) T {
if value > max {
return max
}
return value
}
func LimitMin[T int | int64 | float32 | float64](value T, min T) T {
if value < min {
return min
}
return value
}
func InRange[T int | int64 | float32 | float64](value T, min T, max T) bool {
return value >= min && value <= max
}
func GetError(err error) string {
if err != nil {
return err.Error()
}
return ""
}
func GetIndexSafe[T any](arr []T, index int) *T {
if index >= len(arr) {
return nil
}
return &arr[index]
}
func All(arr ...bool) bool {
for _, v := range arr {
if !v {
return false
}
}
return true
}
func Any(arr ...bool) bool {
for _, v := range arr {
if v {
return true
}
}
return false
}
func Range(start int, end int) []int {
var res []int
for i := start; i < end; i++ {
res = append(res, i)
}
return res
}