mirror of
https://github.com/samber/lo.git
synced 2025-10-28 18:23:05 +08:00
parallel: remove mutex from Map and Times
The slice is pre-allocated, a mutex is not needed.
This commit is contained in:
@@ -7,17 +7,14 @@ import "sync"
|
|||||||
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
|
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
|
||||||
result := make([]R, len(collection))
|
result := make([]R, len(collection))
|
||||||
|
|
||||||
var mu sync.Mutex
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(len(collection))
|
wg.Add(len(collection))
|
||||||
|
|
||||||
for i, item := range collection {
|
for i, item := range collection {
|
||||||
go func (_item T, _i int) {
|
go func(_item T, _i int) {
|
||||||
res := iteratee(_item, _i)
|
res := iteratee(_item, _i)
|
||||||
|
|
||||||
mu.Lock()
|
|
||||||
result[_i] = res
|
result[_i] = res
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}(item, i)
|
}(item, i)
|
||||||
@@ -35,7 +32,7 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
|
|||||||
wg.Add(len(collection))
|
wg.Add(len(collection))
|
||||||
|
|
||||||
for i, item := range collection {
|
for i, item := range collection {
|
||||||
go func (_item T, _i int) {
|
go func(_item T, _i int) {
|
||||||
iteratee(_item, _i)
|
iteratee(_item, _i)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}(item, i)
|
}(item, i)
|
||||||
@@ -50,7 +47,6 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
|
|||||||
func Times[T any](count int, iteratee func(int) T) []T {
|
func Times[T any](count int, iteratee func(int) T) []T {
|
||||||
result := make([]T, count)
|
result := make([]T, count)
|
||||||
|
|
||||||
var mu sync.Mutex
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(count)
|
wg.Add(count)
|
||||||
|
|
||||||
@@ -58,9 +54,7 @@ func Times[T any](count int, iteratee func(int) T) []T {
|
|||||||
go func(_i int) {
|
go func(_i int) {
|
||||||
item := iteratee(_i)
|
item := iteratee(_i)
|
||||||
|
|
||||||
mu.Lock()
|
|
||||||
result[_i] = item
|
result[_i] = item
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}(i)
|
}(i)
|
||||||
@@ -81,7 +75,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
|
|||||||
wg.Add(len(collection))
|
wg.Add(len(collection))
|
||||||
|
|
||||||
for _, item := range collection {
|
for _, item := range collection {
|
||||||
go func (_item T) {
|
go func(_item T) {
|
||||||
key := iteratee(_item)
|
key := iteratee(_item)
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
@@ -106,7 +100,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
|
|||||||
// determined by the order they occur in collection. The grouping is generated from the results
|
// determined by the order they occur in collection. The grouping is generated from the results
|
||||||
// of running each element of collection through iteratee.
|
// of running each element of collection through iteratee.
|
||||||
// `iteratee` is call in parallel.
|
// `iteratee` is call in parallel.
|
||||||
func PartitionBy[T any, K comparable](collection []T, iteratee func (x T) K) [][]T {
|
func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T {
|
||||||
result := [][]T{}
|
result := [][]T{}
|
||||||
seen := map[K]int{}
|
seen := map[K]int{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user