Skip to content

Commit ce12685

Browse files
committed
fix(groupbymap): remove second iteratee and add test+example+doc
1 parent 19cb991 commit ce12685

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Supported helpers for slices:
9292
- [Uniq](#uniq)
9393
- [UniqBy](#uniqby)
9494
- [GroupBy](#groupby)
95+
- [GroupByMap](#groupbymap)
9596
- [Chunk](#chunk)
9697
- [PartitionBy](#partitionby)
9798
- [Flatten](#flatten)
@@ -566,6 +567,19 @@ lop.GroupBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
566567
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
567568
```
568569

570+
### GroupByMap
571+
572+
Returns an object composed of keys generated from the results of running each element of collection through iteratee.
573+
574+
```go
575+
import lo "github.com/samber/lo"
576+
577+
groups := lo.GroupByMap([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, int) {
578+
return i%3, i*2
579+
})
580+
// map[int][]int{0: []int{0, 6}, 1: []int{2, 8}, 2: []int{4, 10}}
581+
```
582+
569583
### Chunk
570584

571585
Returns an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

slice.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,15 @@ func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(it
188188
return result
189189
}
190190

191-
192-
193-
// GroupByMapValues returns an object composed of keys generated from the results of running each element of collection through iterateeKey and values running each element through iterateeValue.
194-
func GroupByMapValues[K comparable, T any, V any](arr []T, iterateeKey func(T) K, iterateeValue func(T) V) map[K][]V {
191+
// GroupByMap returns an object composed of keys generated from the results of running each element of collection through iteratee.
192+
func GroupByMap[T any, K comparable, V any](arr []T, iteratee func(item T) (K, V)) map[K][]V {
195193
result := map[K][]V{}
196194

197195
for _, item := range arr {
198-
k := iterateeKey(item)
199-
v := iterateeValue(item)
200-
196+
k, v := iteratee(item)
201197
result[k] = append(result[k], v)
202198
}
199+
203200
return result
204201
}
205202

slice_example_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ func ExampleGroupBy() {
165165
// [2 5]
166166
}
167167

168+
func ExampleGroupByMap() {
169+
list := []int{0, 1, 2, 3, 4, 5}
170+
171+
result := GroupByMap(list, func(i int) (int, int) {
172+
return i % 3, i * 2
173+
})
174+
175+
fmt.Printf("%v\n", result[0])
176+
fmt.Printf("%v\n", result[1])
177+
fmt.Printf("%v\n", result[2])
178+
// Output:
179+
// [0 6]
180+
// [2 8]
181+
// [4 10]
182+
}
183+
168184
func ExampleChunk() {
169185
list := []int{0, 1, 2, 3, 4}
170186

slice_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ func TestGroupBy(t *testing.T) {
254254
is.IsType(nonempty[42], allStrings, "type preserved")
255255
}
256256

257+
func TestGroupByMap(t *testing.T) {
258+
t.Parallel()
259+
is := assert.New(t)
260+
261+
result1 := GroupByMap([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, string) {
262+
return i % 3, strconv.Itoa(i)
263+
})
264+
265+
is.Equal(len(result1), 3)
266+
is.Equal(result1, map[int][]string{
267+
0: {"0", "3"},
268+
1: {"1", "4"},
269+
2: {"2", "5"},
270+
})
271+
}
272+
257273
func TestChunk(t *testing.T) {
258274
t.Parallel()
259275
is := assert.New(t)

0 commit comments

Comments
 (0)