Skip to content

Commit

Permalink
Implemented GroupByMapValues
Browse files Browse the repository at this point in the history
  • Loading branch information
maryum375 committed Apr 18, 2024
1 parent 71d8341 commit 9baab64
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U
return result
}



// GroupByMapValues returns an object composed of keys generated from the results of running each element of collection through iterateeKey and values running each element through iterateeValue.
func GroupByMapValues[K comparable, T any, V any](arr []T, iterateeKey func(T) K, iterateeValue func(T) V) map[K][]V {
result := map[K][]V{}

for _, item := range arr {
k := iterateeKey(item)
v := iterateeValue(item)

result[k] = append(result[k], v)
}
return result
}

// Chunk returns an array of elements split into groups the length of size. If array can't be split evenly,
// the final chunk will be the remaining elements.
// Play: https://go.dev/play/p/EeKl0AuTehH
Expand Down

0 comments on commit 9baab64

Please sign in to comment.