mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 09:12:28 +08:00
feat: add UniqueBy function in slice.go
This commit is contained in:
@@ -582,6 +582,21 @@ func Unique[T any](slice []T) []T {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
|
||||||
|
func UniqueBy[T any](slice []T, iteratee func(item T) T) []T {
|
||||||
|
if len(slice) == 0 {
|
||||||
|
return []T{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var res []T
|
||||||
|
for _, v := range slice {
|
||||||
|
val := iteratee(v)
|
||||||
|
res = append(res, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Unique(res)
|
||||||
|
}
|
||||||
|
|
||||||
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||||
func Union[T any](slices ...[]T) []T {
|
func Union[T any](slices ...[]T) []T {
|
||||||
if len(slices) == 0 {
|
if len(slices) == 0 {
|
||||||
|
|||||||
@@ -395,6 +395,15 @@ func TestUnique(t *testing.T) {
|
|||||||
assert.Equal([]string{"a", "b", "c"}, Unique([]string{"a", "a", "b", "c"}))
|
assert.Equal([]string{"a", "b", "c"}, Unique([]string{"a", "a", "b", "c"}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUniqueBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUniqueBy")
|
||||||
|
|
||||||
|
actual := UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
|
||||||
|
return val % 4
|
||||||
|
})
|
||||||
|
assert.Equal([]int{1, 2, 3, 0}, actual)
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnion(t *testing.T) {
|
func TestUnion(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestUnion")
|
assert := internal.NewAssert(t, "TestUnion")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user