diff --git a/README.md b/README.md index bfa1a01..2994049 100644 --- a/README.md +++ b/README.md @@ -196,7 +196,7 @@ The corresponding chained function is `collect.UseSlice()` -- `Unique` removes duplicate elements from slices +- `Unique` removes duplicate elements in the slice
Examples @@ -208,6 +208,18 @@ The corresponding chained function is `collect.UseSlice()`
+- `Duplicates` gets the duplicate elements in the slice + +
+ Examples + + ```go + d := []string{"a", "b", "a", "c"} + collect.Duplicates(d) // map[int]string{2: "a"} + ``` + +
+ - `Merge` merges the current slice with other slices
@@ -830,7 +842,7 @@ Due to Golang's support for generics, it is [not possible to define generic type
-- `Times` creates a new collection of slices by calling the callback with specified number of times +- `Times` creates a new collection of slice by calling the callback with specified number of times
Examples diff --git a/README_zh-CN.md b/README_zh-CN.md index 7a45ad5..97cd170 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -208,6 +208,18 @@ import collect "github.com/sxyazi/go-collection"
+- Duplicates:获取切片中的重复元素 + +
+ 例子 + + ```go + d := []string{"a", "b", "a", "c"} + collect.Duplicates() // map[int]string{2: "a"} + ``` + +
+ - Merge:将当前切片与其它切片合并
diff --git a/converter.go b/converter.go index 3cbebc3..bec4762 100644 --- a/converter.go +++ b/converter.go @@ -90,7 +90,7 @@ func OffsetToIndex(actual, offset int, args ...int) (int, int) { return offset, offset + length } -func NumberFrom[N constraints.Integer | constraints.Float, T ~[]E, E comparable](c *SliceCollection[T, E]) *NumberCollection[[]N, N] { +func NumberFrom[N constraints.Integer | constraints.Float, T ~[]E, E any](c *SliceCollection[T, E]) *NumberCollection[[]N, N] { if c.Empty() { return &NumberCollection[[]N, N]{} } diff --git a/functional.go b/functional.go index bfd35d3..0dd5f1e 100644 --- a/functional.go +++ b/functional.go @@ -567,7 +567,7 @@ func Count[T ~[]E, E comparable](items T) map[E]int { return times } -func Times[T ~[]E, E any](number int, callback func(number int) E) *SliceCollection[T, E] { +func Times[T []E, E any](number int, callback func(number int) E) *SliceCollection[T, E] { items := make(T, number) for i := 0; i < number; i++ { items[i] = callback(i + 1) diff --git a/tests/slice_test.go b/tests/slice_test.go index 784dc8a..499bf96 100644 --- a/tests/slice_test.go +++ b/tests/slice_test.go @@ -254,6 +254,9 @@ func TestSlice_Duplicates(t *testing.T) { if !UseSlice([]int{1, 2, 2, 3}).Duplicates().Same(map[int]int{2: 2}) { t.Fail() } + if !UseSlice([]string{"a", "b", "a", "c"}).Duplicates().Same(map[int]string{2: "a"}) { + t.Fail() + } s1, s2 := []int{1, 2, 3}, []int{4, 5, 6} if !UseSlice([][]int{s1, s2, s1}).Duplicates().Same(map[int][]int{2: s1}) { diff --git a/tests/types.go b/tests/types.go index bf59e8a..0529d16 100644 --- a/tests/types.go +++ b/tests/types.go @@ -1,7 +1,7 @@ package tests type Foo struct { - Bar any + Bar string } type User struct {