fix: renaming SliceToSet to Keyify

This commit is contained in:
Samuel Berthe
2025-01-25 23:26:53 +01:00
parent dd2eaa966a
commit 27d2554396
4 changed files with 25 additions and 25 deletions

View File

@@ -102,7 +102,7 @@ Supported helpers for slices:
- [RepeatBy](#repeatby)
- [KeyBy](#keyby)
- [Associate / SliceToMap](#associate-alias-slicetomap)
- [SliceToSet](#slicetoset)
- [Keyify](#keyify)
- [Drop](#drop)
- [DropRight](#dropright)
- [DropWhile](#dropwhile)
@@ -764,12 +764,12 @@ aMap := lo.Associate(in, func (f *foo) (string, int) {
[[play](https://go.dev/play/p/WHa2CfMO3Lr)]
### SliceToSet
### Keyify
Returns a map with each unique element of the slice as a key.
```go
set := lo.SliceToSet([]int{1, 1, 2, 3, 4})
set := lo.Keyify([]int{1, 1, 2, 3, 4})
// map[int]struct{}{1:{}, 2:{}, 3:{}, 4:{}}
```

View File

@@ -388,8 +388,8 @@ func SliceToMap[T any, K comparable, V any](collection []T, transform func(item
return Associate(collection, transform)
}
// SliceToSet returns a map with each unique element of the slice as a key.
func SliceToSet[T comparable, Slice ~[]T](collection Slice) map[T]struct{} {
// Keyify returns a map with each unique element of the slice as a key.
func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{} {
result := make(map[T]struct{}, len(collection))
for _, item := range collection {

View File

@@ -287,6 +287,22 @@ func ExampleAssociate() {
// Output: map[a:1 aa:2 aaa:3]
}
func ExampleKeyify() {
list := []string{"a", "a", "b", "b", "d"}
set := Keyify(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
// Output:
// true
// false
// map[a:{} b:{} d:{}]
}
func ExampleDrop() {
list := []int{0, 1, 2, 3, 4, 5}
@@ -496,19 +512,3 @@ func ExampleIsSortedByKey() {
// Output: true
}
func ExampleSliceToSet() {
list := []string{"a", "a", "b", "b", "d"}
set := SliceToSet(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
// Output:
// true
// false
// map[a:{} b:{} d:{}]
}

View File

@@ -531,13 +531,13 @@ func TestSliceToMap(t *testing.T) {
}
}
func TestSliceToSet(t *testing.T) {
func TestKeyify(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := SliceToSet([]int{1, 2, 3, 4})
result2 := SliceToSet([]int{1, 1, 1, 2})
result3 := SliceToSet([]int{})
result1 := Keyify([]int{1, 2, 3, 4})
result2 := Keyify([]int{1, 1, 1, 2})
result3 := Keyify([]int{})
is.Equal(result1, map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}})
is.Equal(result2, map[int]struct{}{1: {}, 2: {}})
is.Equal(result3, map[int]struct{}{})