Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SliceToSet #514

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Supported helpers for slices:
- [RepeatBy](#repeatby)
- [KeyBy](#keyby)
- [Associate / SliceToMap](#associate-alias-slicetomap)
- [SliceToSet](#slicetoset)
- [Drop](#drop)
- [DropRight](#dropright)
- [DropWhile](#dropwhile)
Expand Down Expand Up @@ -728,6 +729,15 @@ aMap := lo.Associate(in, func (f *foo) (string, int) {

[[play](https://go.dev/play/p/WHa2CfMO3Lr)]

### SliceToSet

Returns a map with each unique element of the slice as a key.

```go
set := lo.SliceToSet([]int{1, 1, 2, 3, 4})
// map[int]struct{}{1:{}, 2:{}, 3:{}, 4:{}}
```

### Drop

Drops n elements from the beginning of a slice or array.
Expand Down
11 changes: 11 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ 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{} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this function in the Readme.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I fixed it! Please review it again.

result := make(map[T]struct{}, len(collection))

for _, item := range collection {
result[item] = struct{}{}
}

return result
}

// Drop drops n elements from the beginning of a slice or array.
// Play: https://go.dev/play/p/JswS7vXRJP2
func Drop[T any, Slice ~[]T](collection Slice, n int) Slice {
Expand Down
18 changes: 18 additions & 0 deletions slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func ExampleForEach() {
// 3
// 4
}

func ExampleForEachWhile() {
list := []int64{1, 2, -math.MaxInt, 4}

Expand All @@ -103,6 +104,7 @@ func ExampleForEachWhile() {
// 1
// 2
}

func ExampleTimes() {
result := Times(3, func(i int) string {
return strconv.FormatInt(int64(i), 10)
Expand Down Expand Up @@ -479,3 +481,19 @@ 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:{}]
}
12 changes: 12 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ func TestSliceToMap(t *testing.T) {
}
}

func TestSliceToSet(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{})
is.Equal(result1, map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}})
is.Equal(result2, map[int]struct{}{1: {}, 2: {}})
is.Equal(result3, map[int]struct{}{})
}

func TestDrop(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down