v0.0.475 ArrGroupBy
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m36s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m36s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user