Skip to content

Instantly share code, notes, and snippets.

@kevinburke kevinburke/experience-unique.md Secret
Last active Aug 25, 2018

Embed
What would you like to do?

Removing the duplicate entries from a slice is cumbersome. In Ruby, you can call .uniq, in Go, you need a ten-line function in your source code.

Ruby: `xs.uniq`
Golang: [a ten-line function written directly in your project's source code]

— Gary Bernhardt (@garybernhardt) September 8, 2017

Similar to the issues that led to math.Round, you can Google for "golang duplicate slice" and read many different explanations of how to implement the function. It might be helpful to have just one in the standard library.

Seriously: google "golang deduplicate slice". Read a hundred responses, each showing a different ten-line function. Again, Ruby: `xs.uniq`.

— Gary Bernhardt (@garybernhardt) September 8, 2017

Some previous discussion can be found here: https://github.com/golang/go/issues/12763

Here's Ruby's uniq: https://ruby-doc.org/core-2.2.0/Array.html#method-i-uniq

@symbiat

This comment has been minimized.

Copy link

commented Jan 12, 2018

The Ruby implementation is > 10 lines :-)

@masklinn

This comment has been minimized.

Copy link

commented Aug 25, 2018

The Ruby implementation is > 10 lines :-)

  1. The Ruby implementation exists once rather than at least once per codebase x slice type (a more type-agnostic version is 30+ LOC)
  2. The Ruby implementation is in C and further the specific C style of MRI
  3. The Ruby implementation handles the additional case of optionally deduplicating by a key function, which no go deduplication snippet does
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.