Skip to content

Commit eb1127a

Browse files
committed
add ProjectSliceToMap function
1 parent fa6183c commit eb1127a

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

pkg/collections/utils.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package collections
22

3-
// ProjectSlice takes a slice and a projection function and applies this function to each element of the slice.
3+
// ProjectSliceToSlice takes a slice and a projection function and applies this function to each element of the slice.
44
// It returns a new slice containing the results of the projection.
55
// The original slice is not modified.
66
// If the projection function is nil, it returns nil.
7+
//
8+
// Deprecated: With the introduction of ProjectSliceToMap, this function has been replaced by ProjectSliceToSlice for better naming consistency.
79
func ProjectSlice[X any, Y any](src []X, project func(X) Y) []Y {
10+
return ProjectSliceToSlice(src, project)
11+
}
12+
13+
// ProjectSliceToSlice takes a slice and a projection function and applies this function to each element of the slice.
14+
// It returns a new slice containing the results of the projection.
15+
// The original slice is not modified.
16+
// If the projection function is nil, it returns nil.
17+
func ProjectSliceToSlice[X any, Y any](src []X, project func(X) Y) []Y {
818
if project == nil {
919
return nil
1020
}
@@ -15,6 +25,23 @@ func ProjectSlice[X any, Y any](src []X, project func(X) Y) []Y {
1525
return res
1626
}
1727

28+
// ProjectSliceToMap takes a slice and a projection function and applies this function to each element of the slice.
29+
// It returns a new map containing the results of the projection.
30+
// The original slice is not modified.
31+
// Note that the resulting map may be smaller if the projection function does not guarantee unique keys.
32+
// If the projection function is nil, it returns nil.
33+
func ProjectSliceToMap[X any, K comparable, V any](src []X, project func(X) (K, V)) map[K]V {
34+
if project == nil {
35+
return nil
36+
}
37+
res := make(map[K]V, len(src))
38+
for _, x := range src {
39+
k, v := project(x)
40+
res[k] = v
41+
}
42+
return res
43+
}
44+
1845
// ProjectMapToSlice takes a map and a projection function and applies this function to each key-value pair in the map.
1946
// It returns a new slice containing the results of the projection.
2047
// The original map is not modified.

pkg/collections/utils_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,54 @@ import (
1212

1313
var _ = Describe("Utils Tests", func() {
1414

15-
Context("ProjectSlice", func() {
15+
Context("ProjectSliceToSlice", func() {
1616

1717
projectFunc := func(i int) int {
1818
return i * 2
1919
}
2020

2121
It("should use the projection function on each element of the slice", func() {
2222
src := []int{1, 2, 3, 4}
23-
projected := collections.ProjectSlice(src, projectFunc)
23+
projected := collections.ProjectSliceToSlice(src, projectFunc)
2424
Expect(projected).To(Equal([]int{2, 4, 6, 8}))
2525
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified")
2626
})
2727

2828
It("should return an empty slice for an empty or nil input slice", func() {
29-
Expect(collections.ProjectSlice(nil, projectFunc)).To(BeEmpty())
30-
Expect(collections.ProjectSlice([]int{}, projectFunc)).To(BeEmpty())
29+
Expect(collections.ProjectSliceToSlice(nil, projectFunc)).To(BeEmpty())
30+
Expect(collections.ProjectSliceToSlice([]int{}, projectFunc)).To(BeEmpty())
3131
})
3232

3333
It("should return nil for a nil projection function", func() {
3434
src := []int{1, 2, 3, 4}
35-
projected := collections.ProjectSlice[int, int](src, nil)
35+
projected := collections.ProjectSliceToSlice[int, int](src, nil)
36+
Expect(projected).To(BeNil())
37+
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified")
38+
})
39+
40+
})
41+
42+
Context("ProjectSliceToMap", func() {
43+
44+
projectFunc := func(i int) (int, int) {
45+
return i, i * 2
46+
}
47+
48+
It("should use the projection function on each element of the slice", func() {
49+
src := []int{1, 2, 3, 4}
50+
projected := collections.ProjectSliceToMap(src, projectFunc)
51+
Expect(projected).To(Equal(map[int]int{1: 2, 2: 4, 3: 6, 4: 8}))
52+
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified")
53+
})
54+
55+
It("should return an empty slice for an empty or nil input slice", func() {
56+
Expect(collections.ProjectSliceToMap(nil, projectFunc)).To(BeEmpty())
57+
Expect(collections.ProjectSliceToMap([]int{}, projectFunc)).To(BeEmpty())
58+
})
59+
60+
It("should return nil for a nil projection function", func() {
61+
src := []int{1, 2, 3, 4}
62+
projected := collections.ProjectSliceToMap[int, int, int](src, nil)
3663
Expect(projected).To(BeNil())
3764
Expect(src).To(Equal([]int{1, 2, 3, 4}), "original slice should not be modified")
3865
})

0 commit comments

Comments
 (0)