mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
5 Commits
572e53aa14
...
730f061b95
| Author | SHA1 | Date | |
|---|---|---|---|
| 730f061b95 | |||
| 2eb08f3404 | |||
| 4c1496b648 | |||
| c2ae784f27 | |||
| e71cecefea |
+101
-1
@@ -4,7 +4,11 @@
|
|||||||
// Package maputil includes some functions to manipulate map.
|
// Package maputil includes some functions to manipulate map.
|
||||||
package maputil
|
package maputil
|
||||||
|
|
||||||
import "reflect"
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
// Keys returns a slice of the map's keys.
|
// Keys returns a slice of the map's keys.
|
||||||
// Play: https://go.dev/play/p/xNB5bTb97Wd
|
// Play: https://go.dev/play/p/xNB5bTb97Wd
|
||||||
@@ -34,6 +38,30 @@ func Values[K comparable, V any](m map[K]V) []V {
|
|||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeysBy creates a slice whose element is the result of function mapper invoked by every map's key.
|
||||||
|
// Play: todo
|
||||||
|
func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T {
|
||||||
|
keys := make([]T, 0, len(m))
|
||||||
|
|
||||||
|
for k := range m {
|
||||||
|
keys = append(keys, mapper(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValuesBy creates a slice whose element is the result of function mapper invoked by every map's value.
|
||||||
|
// Play: todo
|
||||||
|
func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T {
|
||||||
|
keys := make([]T, 0, len(m))
|
||||||
|
|
||||||
|
for _, v := range m {
|
||||||
|
keys = append(keys, mapper(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
// Merge maps, next key will overwrite previous key.
|
// Merge maps, next key will overwrite previous key.
|
||||||
// Play: https://go.dev/play/p/H95LENF1uB-
|
// Play: https://go.dev/play/p/H95LENF1uB-
|
||||||
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
@@ -69,6 +97,32 @@ func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool)
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FilterByKeys iterates over map, return a new map whose keys are all given keys.
|
||||||
|
// Play: todo
|
||||||
|
func FilterByKeys[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
|
||||||
|
}
|
||||||
|
|
||||||
|
// FilterByValues iterates over map, return a new map whose values are all given values.
|
||||||
|
// Play: todo
|
||||||
|
func FilterByValues[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 {
|
||||||
@@ -126,3 +180,49 @@ func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Entry is a key/value pairs.
|
||||||
|
type Entry[K comparable, V any] struct {
|
||||||
|
Key K
|
||||||
|
Value V
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entries transforms a map into array of key/value pairs.
|
||||||
|
// Play: todo
|
||||||
|
func Entries[K comparable, V any](m map[K]V) []Entry[K, V] {
|
||||||
|
entries := make([]Entry[K, V], 0, len(m))
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
entries = append(entries, Entry[K, V]{
|
||||||
|
Key: k,
|
||||||
|
Value: v,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromEntries creates a map based on a slice of key/value pairs
|
||||||
|
// Play: todo
|
||||||
|
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V {
|
||||||
|
result := make(map[K]V, len(entries))
|
||||||
|
|
||||||
|
for _, v := range entries {
|
||||||
|
result[v.Key] = v.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform a map to another type map.
|
||||||
|
// Play: todo
|
||||||
|
func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2 {
|
||||||
|
result := make(map[K2]V2, len(m))
|
||||||
|
|
||||||
|
for k1, v1 := range m {
|
||||||
|
k2, v2 := iteratee(k1, v1)
|
||||||
|
result[k2] = v2
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package maputil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -41,6 +42,51 @@ func TestValues(t *testing.T) {
|
|||||||
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKeysBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestKeysBy")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := KeysBy(m, func(n int) int {
|
||||||
|
return n + 1
|
||||||
|
})
|
||||||
|
|
||||||
|
sort.Ints(keys)
|
||||||
|
|
||||||
|
assert.Equal([]int{2, 3, 4}, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValuesBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestValuesBy")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
3: "c",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := ValuesBy(m, func(v string) string {
|
||||||
|
switch v {
|
||||||
|
case "a":
|
||||||
|
return "a-1"
|
||||||
|
case "b":
|
||||||
|
return "b-2"
|
||||||
|
case "c":
|
||||||
|
return "c-3"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
assert.Equal([]string{"a-1", "b-2", "c-3"}, values)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMerge(t *testing.T) {
|
func TestMerge(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestMerge")
|
assert := internal.NewAssert(t, "TestMerge")
|
||||||
|
|
||||||
@@ -104,6 +150,44 @@ func TestFilter(t *testing.T) {
|
|||||||
}, acturl)
|
}, acturl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterByKeys(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFilterByKeys")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := FilterByKeys(m, []string{"a", "b"})
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterByValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFilterByValues")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := FilterByValues(m, []int{3, 4})
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntersect(t *testing.T) {
|
func TestIntersect(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIntersect")
|
assert := internal.NewAssert(t, "TestIntersect")
|
||||||
|
|
||||||
@@ -170,3 +254,60 @@ func TestIsDisjoint(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(false, IsDisjoint(m1, m3))
|
assert.Equal(false, IsDisjoint(m1, m3))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEntries(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEntries")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := Entries(m)
|
||||||
|
|
||||||
|
sort.Slice(result, func(i, j int) bool {
|
||||||
|
return result[i].Value < result[j].Value
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := []Entry[string, int]{{Key: "a", Value: 1}, {Key: "b", Value: 2}, {Key: "c", Value: 3}}
|
||||||
|
|
||||||
|
assert.Equal(expected, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFromEntries(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFromEntries")
|
||||||
|
|
||||||
|
result := FromEntries([]Entry[string, int]{
|
||||||
|
{Key: "a", Value: 1},
|
||||||
|
{Key: "b", Value: 2},
|
||||||
|
{Key: "c", Value: 3},
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(3, len(result))
|
||||||
|
assert.Equal(1, result["a"])
|
||||||
|
assert.Equal(2, result["b"])
|
||||||
|
assert.Equal(3, result["c"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTransform(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTransform")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := Transform(m, func(k string, v int) (string, string) {
|
||||||
|
return k, strconv.Itoa(v)
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := map[string]string{
|
||||||
|
"a": "1",
|
||||||
|
"b": "2",
|
||||||
|
"c": "3",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(expected, result)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user