feat: adding lo.PartitionBy + lop.PartitionBy + lo.GroupBy

This commit is contained in:
Samuel Berthe
2022-03-05 18:44:39 +01:00
parent 9524c046c6
commit 0c3545daf3
7 changed files with 220 additions and 16 deletions
+67
View File
@@ -43,3 +43,70 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
wg.Wait()
}
// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
// `iteratee` is call in parallel.
func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
result := map[U][]T{}
var mu sync.Mutex
var wg sync.WaitGroup
wg.Add(len(collection))
for _, item := range collection {
go func (_item T) {
key := iteratee(_item)
mu.Lock()
if _, ok := result[key]; !ok {
result[key] = []T{}
}
result[key] = append(result[key], _item)
mu.Unlock()
wg.Done()
}(item)
}
wg.Wait()
return result
}
// PartitionBy returns an array of elements split into groups. The order of grouped values is
// determined by the order they occur in collection. The grouping is generated from the results
// of running each element of collection through iteratee.
// `iteratee` is call in parallel.
func PartitionBy[T any, K comparable](collection []T, iteratee func (x T) K) [][]T {
result := [][]T{}
seen := map[K]int{}
var mu sync.Mutex
var wg sync.WaitGroup
wg.Add(len(collection))
for _, item := range collection {
go func(_item T) {
key := iteratee(_item)
mu.Lock()
resultIndex, ok := seen[key]
if !ok {
resultIndex = len(result)
seen[key] = resultIndex
result = append(result, []T{})
}
result[resultIndex] = append(result[resultIndex], _item)
mu.Unlock()
}(item)
}
wg.Wait()
return result
}