Skip to content

Commit ab4e214

Browse files
authored
Merge pull request #13 from fsouza/remove-mutex
parallel: remove mutex from Map and Times
2 parents 094b6f9 + 770dff2 commit ab4e214

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

map_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func TestEntries(t *testing.T) {
3030

3131
r1 := Entries[string, int](map[string]int{"foo": 1, "bar": 2})
3232

33+
sort.Slice(r1, func(i, j int) bool {
34+
return r1[i].Value < r1[j].Value
35+
})
3336
is.EqualValues(r1, []Entry[string, int]{
3437
{
3538
Key: "foo",

parallel/slice.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ import "sync"
77
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
88
result := make([]R, len(collection))
99

10-
var mu sync.Mutex
1110
var wg sync.WaitGroup
1211
wg.Add(len(collection))
1312

1413
for i, item := range collection {
15-
go func (_item T, _i int) {
14+
go func(_item T, _i int) {
1615
res := iteratee(_item, _i)
1716

18-
mu.Lock()
1917
result[_i] = res
20-
mu.Unlock()
2118

2219
wg.Done()
2320
}(item, i)
@@ -35,7 +32,7 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
3532
wg.Add(len(collection))
3633

3734
for i, item := range collection {
38-
go func (_item T, _i int) {
35+
go func(_item T, _i int) {
3936
iteratee(_item, _i)
4037
wg.Done()
4138
}(item, i)
@@ -50,17 +47,14 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
5047
func Times[T any](count int, iteratee func(int) T) []T {
5148
result := make([]T, count)
5249

53-
var mu sync.Mutex
5450
var wg sync.WaitGroup
5551
wg.Add(count)
5652

5753
for i := 0; i < count; i++ {
5854
go func(_i int) {
5955
item := iteratee(_i)
6056

61-
mu.Lock()
6257
result[_i] = item
63-
mu.Unlock()
6458

6559
wg.Done()
6660
}(i)
@@ -81,7 +75,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
8175
wg.Add(len(collection))
8276

8377
for _, item := range collection {
84-
go func (_item T) {
78+
go func(_item T) {
8579
key := iteratee(_item)
8680

8781
mu.Lock()
@@ -91,7 +85,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
9185
}
9286

9387
result[key] = append(result[key], _item)
94-
88+
9589
mu.Unlock()
9690
wg.Done()
9791
}(item)
@@ -106,7 +100,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
106100
// determined by the order they occur in collection. The grouping is generated from the results
107101
// of running each element of collection through iteratee.
108102
// `iteratee` is call in parallel.
109-
func PartitionBy[T any, K comparable](collection []T, iteratee func (x T) K) [][]T {
103+
func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T {
110104
result := [][]T{}
111105
seen := map[K]int{}
112106

0 commit comments

Comments
 (0)