This commit is contained in:
Samuel Berthe
2022-03-06 00:21:23 +01:00
parent 74d4363ffc
commit 28bec027ee
2 changed files with 21 additions and 2 deletions

View File

@@ -130,6 +130,7 @@ func PartitionBy[T any, K comparable](collection []T, iteratee func (x T) K) [][
result[resultIndex] = append(result[resultIndex], _item) result[resultIndex] = append(result[resultIndex], _item)
mu.Unlock() mu.Unlock()
wg.Done()
}(item) }(item)
} }

View File

@@ -1,8 +1,9 @@
package parallel package parallel
import ( import (
"testing" "sort"
"strconv" "strconv"
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@@ -41,6 +42,13 @@ func TestGroupBy(t *testing.T) {
return i % 3 return i % 3
}) })
// order
for x := range result1 {
sort.Slice(result1[x], func(i, j int) bool {
return result1[x][i] < result1[x][j]
})
}
is.EqualValues(len(result1), 3) is.EqualValues(len(result1), 3)
is.EqualValues(result1, map[int][]int{ is.EqualValues(result1, map[int][]int{
0: []int{0, 3}, 0: []int{0, 3},
@@ -64,6 +72,16 @@ func TestPartitionBy(t *testing.T) {
result1 := PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, oddEven) result1 := PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, oddEven)
result2 := PartitionBy[int, string]([]int{}, oddEven) result2 := PartitionBy[int, string]([]int{}, oddEven)
is.Equal(result1, [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}) // order
sort.Slice(result1, func(i, j int) bool {
return result1[i][0] < result1[j][0]
})
for x := range result1 {
sort.Slice(result1[x], func(i, j int) bool {
return result1[x][i] < result1[x][j]
})
}
is.ElementsMatch(result1, [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}})
is.Equal(result2, [][]int{}) is.Equal(result2, [][]int{})
} }