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: slices: Unique() #66247

Closed
antonsacred opened this issue Mar 11, 2024 · 3 comments
Closed

proposal: slices: Unique() #66247

antonsacred opened this issue Mar 11, 2024 · 3 comments
Labels
Milestone

Comments

@antonsacred
Copy link

Proposal Details

I propose to create function Unique with the definition:

// Unique replaces runs of equal elements with a single copy.
// Unique modifies the contents of the slice s and returns the modified slice,
// which may have a smaller length.
func Unique[S ~[]E, E comparable](s S) S {}

and yes, we already have Compact, but Compact can not filter items that are not consecutive.

Example of usage:

myslice := []int{1,2,1}
filteredslice := slises.Unique(myslice) // []int{1,2}

I have already created PR for this change #66231

@gopherbot gopherbot added this to the Proposal milestone Mar 11, 2024
@seankhliao
Copy link
Member

This was discussed in #47203 and #45955 and it is not something that we will do.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
@xin-hao-awx
Copy link

But the Golang repo already has it as an internal method, although not a generic version.
https://github.com/golang/go/blob/master/src/go/build/build.go#L1109
Uniq and UniqFunc are very high-frequency methods.

@DeedleFake
Copy link

DeedleFake commented Sep 20, 2024

@xin-hao-awx

The function that you linked to just does what slices.Compact() does and removes consecutive duplicates, not all duplicates.

Edit: Sorry, no it doesn't. I missed that it sorted the slice first.

I have a Uniq() method for iterators implemented in my iterator support package if you want to use that, but it's not exactly complicated to write.

Uniq and UniqFunc are very high-frequency methods.

I'm curious why you would make that contention. I've only needed them a handful of times ever as far as I remember.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants