Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maps: new package to provide generic map functions #47649

Open
rsc opened this issue Aug 11, 2021 Discussed in #47330 · 15 comments
Open

maps: new package to provide generic map functions #47649

rsc opened this issue Aug 11, 2021 Discussed in #47330 · 15 comments

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Aug 11, 2021

Discussed in #47330

// Package maps defines various functions useful with maps of any type.
package maps

// Keys returns the keys of the map m.
// The keys will be an indeterminate order.
func Keys[M constraints.Map[K, V], K comparable, V any](m M) []K

// Values returns the values of the map m.
// The values will be in an indeterminate order.
func Values[M constraints.Map[K, V], K comparable, V any](m M) []V

// Equal reports whether two maps contain the same key/value pairs.
// Values are compared using ==.
func Equal[M1, M2 constraints.Map[K, V], K, V comparable](m1 M1, m2 M2) bool

// EqualFunc is like Equal, but compares values using cmp.
// Keys are still compared with ==.
func EqualFunc[M1 constraints.Map[K, V1], M2 constraints.Map[K, V2], K comparable, V1, V2 any](m1 m1, m2 M2, cmp func(V1, V2) bool) bool

// Clear removes all entries from m, leaving it empty.
func Clear[M constraints.Map[K, V], K comparable, V any](m M)

// Clone returns a copy of m.  This is a shallow clone:
// the new keys and values are set using ordinary assignment.
func Clone[M constraints.Map[K, V], K comparable, V any](m M) 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.
func Copy[M constraints.Map[K, V], K comparable, V any](dst, src M)

// DeleteFunc deletes any key/value pairs from m for which del returns true.
func DeleteFunc[M constraints.Map[K, V], K comparable, V any](m M, del func(K, V) bool)
@rsc rsc added the generics label Aug 11, 2021
@gopherbot gopherbot added this to the Proposal milestone Aug 11, 2021
@rsc rsc added this to Active in Proposals Aug 11, 2021
@cespare cespare changed the title proposal: maps: new package to provide generic map functions (discussion) proposal: maps: new package to provide generic map functions Aug 11, 2021
@hherman1
Copy link

@hherman1 hherman1 commented Aug 11, 2021

These look pretty good to me, I only really paused over the Keys and Values functions: how often are these used where you wouldn’t be better served by a for range loop?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 11, 2021

I think that Keys and Values would be used quite a lot. For example, Keys is naturally used when removing duplicate elements from a slice.

func RemoveDuplicates(s []someType) []someType {
    m := make(map[someType]bool)
    for _, v := range s {
        m[v] = true
    }
    return maps.Keys(m)

@tusharsingh
Copy link

@tusharsingh tusharsingh commented Aug 11, 2021

Instead of Add have you considered that Copy may be clearer? Copy implies copying from a src to dst and overwriting values.

@pcman312
Copy link

@pcman312 pcman312 commented Aug 11, 2021

Add is a bit unclear in my opinion because it makes it sound like you're adding an individual entry (or entries). What about Merge instead?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 11, 2021

It would be best if discussion about this proposal continues on the existing discussion at #47330. Decisions made there will be reflected here. I'm going to lock this issue temporarily. Thanks.

@golang golang locked and limited conversation to collaborators Aug 11, 2021
@rsc
Copy link
Contributor Author

@rsc rsc commented Aug 25, 2021

Discussion on #47330 seems to have converged. I've copied the latest API into the top comment on this issue. Any comments should still be made on #47330, not here.

@rsc
Copy link
Contributor Author

@rsc rsc commented Aug 25, 2021

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Accept in Proposals Aug 25, 2021
@rsc rsc moved this from Likely Accept to Active in Proposals Sep 1, 2021
@rsc
Copy link
Contributor Author

@rsc rsc commented Sep 1, 2021

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
Copy link
Contributor Author

@rsc rsc commented Sep 8, 2021

We've discussed and agreed to change DeleteIf to DeleteFunc to better match other APIs we have.

@rsc
Copy link
Contributor Author

@rsc rsc commented Sep 8, 2021

Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Accept in Proposals Sep 8, 2021
@rsc
Copy link
Contributor Author

@rsc rsc commented Sep 15, 2021

Still some discussion on #47330 but it seems to be converging.

@rsc
Copy link
Contributor Author

@rsc rsc commented Sep 22, 2021

Converged to using constraints.Map consistently.

@rsc rsc moved this from Likely Accept to Accepted in Proposals Sep 22, 2021
@rsc
Copy link
Contributor Author

@rsc rsc commented Sep 22, 2021

No change in consensus, so accepted. 🎉
This issue now tracks the work of implementing the proposal.
— rsc for the proposal review group

@rsc rsc changed the title proposal: maps: new package to provide generic map functions maps: new package to provide generic map functions Sep 22, 2021
@rsc rsc removed this from the Proposal milestone Sep 22, 2021
@rsc rsc added this to the Backlog milestone Sep 22, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 17, 2021

Change https://golang.org/cl/364875 mentions this issue: maps: new package

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 19, 2021

This package is now available at golang.org/x/exp/maps. Per https://groups.google.com/g/golang-dev/c/iuB22_G9Kbo/m/7B1jd1I3BQAJ?utm_medium=email&utm_source=footer it will not be put into standard library until the 1.19 release. We may of course adjust it based on anything we learn about having it in x/exp.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Proposals
Accepted
Development

No branches or pull requests

6 participants