v0.0.475 ArrGroupBy
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m36s

This commit is contained in:
2024-06-16 17:14:21 +02:00
parent d2f2a0558a
commit 78e1c33e30
4 changed files with 20 additions and 3 deletions

View File

@@ -564,3 +564,18 @@ func ArrChunk[T any](arr []T, chunkSize int) [][]T {
return res
}
func ArrGroupBy[T1 any, T2 comparable](arr []T1, groupfunc func(v T1) T2) map[T2][]T1 {
r := make(map[T2][]T1)
for _, v := range arr {
key := groupfunc(v)
if _, ok := r[key]; ok {
r[key] = append(r[key], v)
} else {
r[key] = []T1{v}
}
}
return r
}