You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.3 KiB
Go

package util
import (
"math/rand"
"strconv"
"time"
"unicode"
)
const alphabet = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func init() {
rand.Seed(time.Now().UnixNano())
}
func GenerateRandomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(b)
}
func IsWhitespaceFree(s string) bool {
for _, r := range s {
if unicode.IsSpace(r) {
return false
}
}
return true
}
func AtoiInto(s string, into *int) error {
num, err := strconv.Atoi(s)
if err != nil {
return err
}
*into = num
return nil
}
func RepeatedSlice[T any](value T, n int) []T {
arr := make([]T, n)
for i := 0; i < n; i++ {
arr[i] = value
}
return arr
}
func Shuffle[T any](slice []T) {
rand.Shuffle(len(slice), func(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
})
}
func Sample[T any](vs []T, count int) []T {
buffer := append([]T{}, vs...)
result := make([]T, count)
for i := 0; i < count; i++ {
n := rand.Intn(len(buffer))
result[i], buffer = buffer[n], append(buffer[:n], buffer[n+1:]...)
}
return result
}
func GroupBy[K comparable, V any](vs []V, keyFn func(V) K) map[K][]V {
r := map[K][]V{}
for _, v := range vs {
key := keyFn(v)
group, ok := r[key]
if !ok {
group = []V{}
}
r[key] = append(group, v)
}
return r
}