1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

feat: add UniqueBy function in slice.go

This commit is contained in:
dudaodong
2022-06-17 15:18:04 +08:00
parent 713c341831
commit d2df99a6f0
2 changed files with 24 additions and 0 deletions

View File

@@ -582,6 +582,21 @@ func Unique[T any](slice []T) []T {
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.
func Union[T any](slices ...[]T) []T {
if len(slices) == 0 {