72 lines
1.0 KiB
Go
72 lines
1.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand/v2"
|
|
"strconv"
|
|
)
|
|
|
|
func RandRange(min, max int) int {
|
|
return rand.IntN(max-min) + min
|
|
}
|
|
func Min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
func Max(a, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func Map[S, R any](f func(s S) R, s []S) []R {
|
|
out := make([]R, len(s))
|
|
for i := range s {
|
|
out[i] = f(s[i])
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Deprecated
|
|
func PopSlice[S any](s []S, index int) []S {
|
|
if index == 0 {
|
|
return s[1:]
|
|
}
|
|
out := make([]S, 0)
|
|
for i, e := range s {
|
|
if i == index {
|
|
continue
|
|
}
|
|
out = append(out, e)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Deprecated
|
|
func TypedSliceToAny[S any](s []S) []any {
|
|
out := make([]any, len(s))
|
|
for i := range s {
|
|
out[i] = s[i]
|
|
}
|
|
return out
|
|
}
|
|
func AppendToInt[S any](v int, s []S) []any {
|
|
out := make([]any, 1, len(s)+1)
|
|
out[0] = v
|
|
out = append(out, TypedSliceToAny(s)...)
|
|
return out
|
|
}
|
|
func StringToInt(s string) int {
|
|
i, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return -1
|
|
}
|
|
return i
|
|
}
|
|
func AnyToString[A any](a A) string {
|
|
return fmt.Sprintf("%v", a)
|
|
}
|