mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
feat: add OmitBy, OmitByKeys, OmitByValues
This commit is contained in:
@@ -123,6 +123,45 @@ func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OmitBy is the opposite of Filter, removes all the map elements for which the predicate function returns true.
|
||||||
|
// Play: todo
|
||||||
|
func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
|
||||||
|
result := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
if !predicate(k, v) {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// OmitByKeys the opposite of FilterByKeys, extracts all the map elements which keys are not omitted.
|
||||||
|
// Play: todo
|
||||||
|
func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V {
|
||||||
|
result := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
if !slice.Contain(keys, k) {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// OmitByValues the opposite of FilterByValues. remov all elements whose value are in the give slice.
|
||||||
|
// Play: todo
|
||||||
|
func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V {
|
||||||
|
result := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
if !slice.Contain(values, v) {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// Intersect iterates over maps, return a new map of key and value pairs in all given maps.
|
// Intersect iterates over maps, return a new map of key and value pairs in all given maps.
|
||||||
// Play: https://go.dev/play/p/Zld0oj3sjcC
|
// Play: https://go.dev/play/p/Zld0oj3sjcC
|
||||||
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
|||||||
@@ -188,6 +188,69 @@ func TestFilterByValues(t *testing.T) {
|
|||||||
}, acturl)
|
}, acturl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOmitBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestOmitBy")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := OmitBy(m, isEven)
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"c": 3,
|
||||||
|
"e": 5,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOmitByKeys(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestOmitByKeys")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := OmitByKeys(m, []string{"a", "b"})
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOmitByValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestOmitByValues")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := OmitByValues(m, []int{4, 5})
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntersect(t *testing.T) {
|
func TestIntersect(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIntersect")
|
assert := internal.NewAssert(t, "TestIntersect")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user