mirror of
https://github.com/samber/lo.git
synced 2025-09-27 04:15:58 +08:00
Perf(slice): Optimize iteration function parameters and add test cases (#587)
- Modify the iteration function parameters in the UniqueBy, GroupByMap, and Keyify functions to use index access for collection elements. - Optimize parameter naming in the GroupByMap function to improve code readability. - Add test cases for the GroupByMap function to cover different types of inputs. Co-authored-by: ShuQingZai <overbeck.jack@outlook.com>
This commit is contained in:
15
slice.go
15
slice.go
@@ -38,8 +38,8 @@ func UniqMap[T any, R comparable](collection []T, iteratee func(item T, index in
|
|||||||
result := make([]R, 0, len(collection))
|
result := make([]R, 0, len(collection))
|
||||||
seen := make(map[R]struct{}, len(collection))
|
seen := make(map[R]struct{}, len(collection))
|
||||||
|
|
||||||
for i, item := range collection {
|
for i := range collection {
|
||||||
r := iteratee(item, i)
|
r := iteratee(collection[i], i)
|
||||||
if _, ok := seen[r]; !ok {
|
if _, ok := seen[r]; !ok {
|
||||||
result = append(result, r)
|
result = append(result, r)
|
||||||
seen[r] = struct{}{}
|
seen[r] = struct{}{}
|
||||||
@@ -189,11 +189,12 @@ func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(it
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GroupByMap returns an object composed of keys generated from the results of running each element of collection through iteratee.
|
// GroupByMap returns an object composed of keys generated from the results of running each element of collection through iteratee.
|
||||||
func GroupByMap[T any, K comparable, V any](arr []T, iteratee func(item T) (K, V)) map[K][]V {
|
func GroupByMap[T any, K comparable, V any](collection []T, iteratee func(item T) (K, V)) map[K][]V {
|
||||||
result := map[K][]V{}
|
result := map[K][]V{}
|
||||||
|
|
||||||
for _, item := range arr {
|
for i := range collection {
|
||||||
k, v := iteratee(item)
|
k, v := iteratee(collection[i])
|
||||||
|
|
||||||
result[k] = append(result[k], v)
|
result[k] = append(result[k], v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,8 +421,8 @@ func FilterSliceToMap[T any, K comparable, V any](collection []T, transform func
|
|||||||
func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{} {
|
func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{} {
|
||||||
result := make(map[T]struct{}, len(collection))
|
result := make(map[T]struct{}, len(collection))
|
||||||
|
|
||||||
for _, item := range collection {
|
for i := range collection {
|
||||||
result[item] = struct{}{}
|
result[collection[i]] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@@ -268,6 +268,41 @@ func TestGroupByMap(t *testing.T) {
|
|||||||
1: {"1", "4"},
|
1: {"1", "4"},
|
||||||
2: {"2", "5"},
|
2: {"2", "5"},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
type myInt int
|
||||||
|
type myInts []myInt
|
||||||
|
result2 := GroupByMap(myInts{1, 0, 2, 3, 4, 5}, func(i myInt) (int, string) {
|
||||||
|
return int(i % 3), strconv.Itoa(int(i))
|
||||||
|
})
|
||||||
|
|
||||||
|
is.Equal(len(result2), 3)
|
||||||
|
is.Equal(result2, map[int][]string{
|
||||||
|
0: {"0", "3"},
|
||||||
|
1: {"1", "4"},
|
||||||
|
2: {"2", "5"},
|
||||||
|
})
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
ID int64
|
||||||
|
CategoryID int64
|
||||||
|
}
|
||||||
|
products := []product{
|
||||||
|
{ID: 1, CategoryID: 1},
|
||||||
|
{ID: 2, CategoryID: 1},
|
||||||
|
{ID: 3, CategoryID: 2},
|
||||||
|
{ID: 4, CategoryID: 3},
|
||||||
|
{ID: 5, CategoryID: 3},
|
||||||
|
}
|
||||||
|
result3 := GroupByMap(products, func(item product) (int64, string) {
|
||||||
|
return item.CategoryID, "Product " + strconv.FormatInt(item.ID, 10)
|
||||||
|
})
|
||||||
|
|
||||||
|
is.Equal(len(result3), 3)
|
||||||
|
is.Equal(result3, map[int64][]string{
|
||||||
|
1: {"Product 1", "Product 2"},
|
||||||
|
2: {"Product 3"},
|
||||||
|
3: {"Product 4", "Product 5"},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChunk(t *testing.T) {
|
func TestChunk(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user