From dde4a8888ee06d829c7e4beb7ce8d1e74b2d1676 Mon Sep 17 00:00:00 2001 From: nicklausliu Date: Thu, 15 Aug 2024 00:07:28 +0800 Subject: [PATCH 1/3] feat: add SliceToSet --- slice.go | 11 +++++++++++ slice_example_test.go | 16 ++++++++++++++++ slice_test.go | 8 ++++++++ 3 files changed, 35 insertions(+) diff --git a/slice.go b/slice.go index d2d3fd84..5973b961 100644 --- a/slice.go +++ b/slice.go @@ -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{} { + 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 { diff --git a/slice_example_test.go b/slice_example_test.go index 0d64d8f0..a66617e6 100644 --- a/slice_example_test.go +++ b/slice_example_test.go @@ -88,6 +88,7 @@ func ExampleForEach() { // 3 // 4 } + func ExampleForEachWhile() { list := []int64{1, 2, -math.MaxInt, 4} @@ -103,6 +104,7 @@ func ExampleForEachWhile() { // 1 // 2 } + func ExampleTimes() { result := Times(3, func(i int) string { return strconv.FormatInt(int64(i), 10) @@ -479,3 +481,17 @@ func ExampleIsSortedByKey() { // Output: true } + +func ExampleSliceToSet() { + list := []string{"a", "b", "d"} + + set := SliceToSet(list) + _, ok1 := set["a"] + _, ok2 := set["c"] + fmt.Printf("%v\n", ok1) + fmt.Printf("%v\n", ok2) + + // Output: + // true + // false +} diff --git a/slice_test.go b/slice_test.go index abb9450e..ace84150 100644 --- a/slice_test.go +++ b/slice_test.go @@ -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) From 3c375bdc0f9434894c038cc73e781d495a8952db Mon Sep 17 00:00:00 2001 From: nicklausliu Date: Sat, 17 Aug 2024 19:53:13 +0800 Subject: [PATCH 2/3] feat: add more duplicate test --- slice_example_test.go | 4 +++- slice_test.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/slice_example_test.go b/slice_example_test.go index a66617e6..6d170141 100644 --- a/slice_example_test.go +++ b/slice_example_test.go @@ -483,15 +483,17 @@ func ExampleIsSortedByKey() { } func ExampleSliceToSet() { - list := []string{"a", "b", "d"} + 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:{}] } diff --git a/slice_test.go b/slice_test.go index ace84150..e8ee0e19 100644 --- a/slice_test.go +++ b/slice_test.go @@ -519,7 +519,11 @@ func TestSliceToSet(t *testing.T) { 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) { From b45857cb9c99d6c2142f6f7b2b87c7114b2c060c Mon Sep 17 00:00:00 2001 From: nicklausliu Date: Sat, 17 Aug 2024 20:04:15 +0800 Subject: [PATCH 3/3] feat: add README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3f73cc8e..a4116367 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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.