lo icon indicating copy to clipboard operation
lo copied to clipboard

Proposal: considering a new type of Group: a key mapper to a bunch of values, for extend map structure

Open AuZhoomLee opened this issue 1 year ago • 0 comments

Usually, we use one to one mapper: a key to a value of map, but we also like to series a dozen of values in a specific way sometimes, the result is a bunch of values mappers to the same key, that will like be:

// key is a specific regx
kv := map[string]string{"key1-1": "a", "key1-2": "b",  "key2-1": "c", "key2-2": "d"}

or

// inner is a slice
kv := map[string][]string{
	"key1": {"a", "b"},
	"key2": {"c", "d"},
}
// or inner is a map
kv := map[string]map[string]struct{}{
	"key1": {
		"a": struct{}{},
		"b": struct{}{},
	},
	"key2": {
		"c": struct{}{},
		"d": struct{}{},
	},
}

url.Values is so like this and supply some helpful method like Add \ Del \ Get \ Has \ Set I also used to find a leaf one from the top root a lot in some collection services, who keeps key indexing data.

So, can we considering a new extend type or some method to operate situation like this? Here's my thought:

type Group[K comparable, V any] map[K][]V

type OrderedGroup[K comparable, S ~[]E, E cmp.Ordered] map[K]S

type UniqGroup[K ,V comparable] map[K]map[V]struct{}

I will supply example implementation later.

AuZhoomLee avatar May 18 '24 19:05 AuZhoomLee