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

proposal: x/exp/slices: addition of Count function #57798

Closed
Noah-Wagner opened this issue Jan 14, 2023 · 6 comments
Closed

proposal: x/exp/slices: addition of Count function #57798

Noah-Wagner opened this issue Jan 14, 2023 · 6 comments

Comments

@Noah-Wagner
Copy link

API

// Count reports the number of elements in s that are equal to v.
func Count[E comparable](s []E, v E) int

For inputs of nil or an empty slice, the expected output is 0. For an input where v is not in s, the expected output is 0. For all other (happy path) cases, the expected output is equal to the number of elements in s that are equal to v.

Rationale

No nuanced rationale here, a couple times in the near past I have been looking for such a Count method when working with slices. Interested in gauging interest of whether such an additional API is worth adding to the exp/slices library. It is trivial to implement when needed, but also a nice convenience function to have.

Note that this proposal can also be easily extended to cover CountFunc if we wish to maintain the usual symmetry that is found in the exp/slices library.

@gopherbot gopherbot added this to the Proposal milestone Jan 14, 2023
@ianlancetaylor
Copy link
Contributor

It would help to point to some existing code that would benefit from this function. Thanks.

@icholy
Copy link

icholy commented Jan 16, 2023

This type of usecase seems like it would be better covered by a x/exp/set package. Something like:

count := set.Intersect(set.Of(a...), set.Of(b...)).Size()

@rsc
Copy link
Contributor

rsc commented Mar 29, 2023

I think we are still waiting on a compelling use case for this.

@rsc
Copy link
Contributor

rsc commented Mar 29, 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
Copy link
Contributor

rsc commented Apr 6, 2023

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

@rsc
Copy link
Contributor

rsc commented Apr 12, 2023

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Apr 12, 2023
@golang golang locked and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants