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 1 commit
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
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
16 changes: 16 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,17 @@ func ExampleIsSortedByKey() {

// Output: true
}

func ExampleSliceToSet() {
list := []string{"a", "b", "d"}
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 more test , in which we have duplicate entries in the slice ?

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.


set := SliceToSet(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)

// Output:
// true
// false
}
8 changes: 8 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ func TestSliceToMap(t *testing.T) {
}
}

func TestSliceToSet(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := SliceToSet([]int{1, 2, 3, 4})
is.Equal(result1, map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}})
}

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