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

feature: slice sub #110

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Changes from all commits
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
20 changes: 13 additions & 7 deletions utils/datautil/datautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ import (
"sort"
)

// SliceSubFunc returns elements in slice a that are not present in slice b (a - b) and remove duplicates.
// Determine if elements are equal based on the result returned by fn.
func SliceSubFunc[T any, E comparable](a, b []T, fn func(i T) E) []T {
// SliceSubFuncs returns elements in slice a that are not present in slice b (a - b) and remove duplicates.
// Determine if elements are equal based on the result returned by fna(a[i]) and fnb(b[i]).
func SliceSubFuncs[T, V any, E comparable](a []T, b []V, fna func(i T) E, fnb func(i V) E) []T {
if len(b) == 0 {
return a
}
k := make(map[E]struct{})
for i := 0; i < len(b); i++ {
k[fn(b[i])] = struct{}{}
k[fnb(b[i])] = struct{}{}
}
t := make(map[E]struct{})
rs := make([]T, 0, len(a))
for i := 0; i < len(a); i++ {
e := fn(a[i])
e := fna(a[i])
if _, ok := t[e]; ok {
continue
}
Expand All @@ -49,6 +49,12 @@ func SliceSubFunc[T any, E comparable](a, b []T, fn func(i T) E) []T {
return rs
}

// SliceSubFunc returns elements in slice a that are not present in slice b (a - b) and remove duplicates.
// Determine if elements are equal based on the result returned by fn.
func SliceSubFunc[T any, E comparable](a, b []T, fn func(i T) E) []T {
return SliceSubFuncs(a, b, fn, fn)
}

// SliceSub returns elements in slice a that are not present in slice b (a - b) and remove duplicates.
func SliceSub[E comparable](a, b []E) []E {
return SliceSubFunc(a, b, func(i E) E { return i })
Expand All @@ -62,8 +68,8 @@ func SliceSubAny[E comparable, T any](a []E, b []T, fn func(t T) E) []E {

// SliceSubConvertPre returns elements in slice a that are not present in slice b (a - b) and remove duplicates.
// fn is a function that converts elements of slice a to elements comparable with those in slice b.
func SliceSubConvertPre[E comparable, T any](a []T, b []E, fn func(t T) E) []E {
return SliceSub(Slice(a, fn), b)
func SliceSubConvertPre[E comparable, T any](a []T, b []E, fn func(t T) E) []T {
return SliceSubFuncs(a, b, fn, func(i E) E { return i })
}

// SliceAnySub returns elements in slice a that are not present in slice b (a - b).
Expand Down
Loading