1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add Minus func

This commit is contained in:
dudaodong
2022-03-31 18:00:11 +08:00
parent bf7ffbfa8d
commit 19939c2b03
2 changed files with 32 additions and 1 deletions

View File

@@ -60,7 +60,7 @@ func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool)
return res
}
// Intersect iterates over maps, return a new map of key and value pairs in all give maps
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
if len(maps) == 0 {
return map[K]V{}
@@ -92,3 +92,16 @@ func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
return res
}
// Minus creates an map of whose key in mapA but not in mapB
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
res := make(map[K]V)
for k, v := range mapA {
if _, ok := mapB[k]; !ok {
res[k] = v
}
}
return res
}

View File

@@ -130,3 +130,21 @@ func TestIntersect(t *testing.T) {
assert.Equal(map[string]int{"a": 1, "b": 2}, Intersect(m1, m2))
assert.Equal(map[string]int{"a": 1}, Intersect(m1, m2, m3))
}
func TestMinus(t *testing.T) {
assert := internal.NewAssert(t, "TestMinus")
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 11,
"b": 22,
"d": 33,
}
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
}