mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
feat: adding lo.PartitionBy + lop.PartitionBy + lo.GroupBy
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user