You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
maps: new standard library package based on x/exp/maps #57436
In #47649@rsc proposed a new maps package in the standard library based on discussion in #47330. After we settled on an API we added the experimental package golang.org/x/exp/maps.
We now have experience with this package in practice. pkg.go.dev reports that it is imported by 615 other packages. I propose that it is now time to move this package into the standard library for the 1.21 release.
Doing this doesn't mean that we can't add more functions to the standard library maps package later. Any such additions should be suggested in their own separate proposal. (There are some existing proposals that would move from x/exp/maps to maps: at least #54012, #54454, #57191).
The specific API that we will put into the standard library is as follows.
// Keys returns the keys of the map m.// The keys will be in an indeterminate order.funcKeys[M~map[K]V, Kcomparable, Vany](mM) []K// Values returns the values of the map m.// The values will be in an indeterminate order.funcValues[M~map[K]V, Kcomparable, Vany](mM) []V// Equal reports whether two maps contain the same key/value pairs.// Values are compared using ==.funcEqual[M1, M2~map[K]V, K, Vcomparable](m1M1, m2M2) bool// EqualFunc is like Equal, but compares values using eq.// Keys are still compared with ==.funcEqualFunc[M1~map[K]V1, M2~map[K]V2, Kcomparable, V1, V2any](m1M1, m2M2, eqfunc(V1, V2) bool) bool// Clone returns a copy of m. This is a shallow clone:// the new keys and values are set using ordinary assignment.funcClone[M~map[K]V, Kcomparable, Vany](mM) M// Copy copies all key/value pairs in src adding them to dst.// When a key in src is already present in dst,// the value in dst will be overwritten by the value associated// with the key in src.funcCopy[M1~map[K]V, M2~map[K]V, Kcomparable, Vany](dstM1, srcM2)
// DeleteFunc deletes any key/value pairs from m for which del returns true.funcDeleteFunc[M~map[K]V, Kcomparable, Vany](mM, delfunc(K, V) bool)
I would love to see either a new type sortedMap, or the option to implement sorting/searching of maps by value on put/delete. This is a builtin type in a handful of other popular programming languages, and since I don't see many people being willing to implement red/black trees on their own, they will have to implement their own linked hash map with a sorted slice, which is generally less efficient in space complexity.
A concern I've had with EqualFunc before is it compares length first, so you can't use it if you want to consider m1 and m2 the same if any keys missing from one or the other have zero values. E.g., these two maps might be considered equivalent for some applications:
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
Given that we already reviewed this API before it went into x/exp, it is not surprising there's not much discussion here. The proposal is exactly x/exp/maps minus Clear.
Does anyone object to accepting this proposal?
Reacted by DeedleFake, Carlana, Hunter Herman, Mitar, Mateusz Poliwczak, محمّد, li1234yun and a-pav
Activity
hherman1 commentedon Dec 22, 2022
Isn’t Clear obsoleted by the new clear builtin?
ianlancetaylor commentedon Dec 22, 2022
Thanks. I think you're right. I've edited the proposal to remove
Clear.iamemilio commentedon Dec 22, 2022
I would love to see either a new type
sortedMap, or the option to implement sorting/searching of maps by value on put/delete. This is a builtin type in a handful of other popular programming languages, and since I don't see many people being willing to implement red/black trees on their own, they will have to implement their own linked hash map with a sorted slice, which is generally less efficient in space complexity.mooijtech commentedon Dec 22, 2022
@iamemilio Seems out of scope for this proposal.
References:
earthboundkid commentedon Dec 23, 2022
A concern I've had with EqualFunc before is it compares length first, so you can't use it if you want to consider m1 and m2 the same if any keys missing from one or the other have zero values. E.g., these two maps might be considered equivalent for some applications:
Maybe the solution is to add a looser but slower function with a name like
Equivalent.leighmcculloch commentedon Dec 26, 2022
rsc commentedon Jan 4, 2023
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
rsc commentedon Jan 11, 2023
@carlmjohnson if you feel strongly about Equivalent being in the package that should be a separate proposal. It seems a little niche to me.
rsc commentedon Jan 18, 2023
Given that we already reviewed this API before it went into x/exp, it is not surprising there's not much discussion here. The proposal is exactly x/exp/maps minus Clear.
Does anyone object to accepting this proposal?
11 remaining items
gopherbot commentedon Feb 1, 2023
Change https://go.dev/cl/464343 mentions this issue:
maps: new packagemaps: new package
gopherbot commentedon May 27, 2023
Change https://go.dev/cl/498602 mentions this issue:
doc/go1.21: mention maps packagedoc/go1.21: mention maps package
jdemeyer commentedon Aug 28, 2023
KeysandValueswere removed: #61538maps: new package
maps: new package
maps: new package