some fixes and changes; #4
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package utils
|
||||
|
||||
import "math/rand/v2"
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func RandRange(min, max int) int {
|
||||
return rand.IntN(max-min) + min
|
||||
@@ -17,8 +21,18 @@ func Max(a, b int) int {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -28,3 +42,26 @@ func PopSlice[S any](s []S, index int) []S {
|
||||
}
|
||||
return out
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user