This commit is contained in:
withchao 2023-02-21 16:22:22 +08:00
parent f5fbe1d852
commit edc09af93f

View File

@ -159,7 +159,7 @@ func Filter[E, T any](es []E, fn func(e E) (T, bool)) []T {
func Slice[E any, T any](es []E, fn func(e E) T) []T {
v := make([]T, len(es))
for i := 0; i < len(es); i++ {
v = append(v, fn(es[i]))
v[i] = fn(es[i])
}
return v
}
@ -358,6 +358,28 @@ func Single[E comparable](a, b []E) []E {
return v
}
// SliceSub a中存在,b中不存在 (a-b)
func SliceSub[E comparable](a, b []E) []E {
k := make(map[E]struct{})
for i := 0; i < len(b); i++ {
k[b[i]] = struct{}{}
}
t := make(map[E]struct{})
rs := make([]E, 0, len(a))
for i := 0; i < len(a); i++ {
e := a[i]
if _, ok := t[e]; ok {
continue
}
if _, ok := k[e]; ok {
continue
}
rs = append(rs, e)
t[e] = struct{}{}
}
return rs
}
// Order 将ts按es排序
func Order[E comparable, T any](es []E, ts []T, fn func(t T) E) []T {
if len(es) == 0 || len(ts) == 0 {