Skip to content

Instantly share code, notes, and snippets.

@kevinburke
Last active August 25, 2018 15:59
Show Gist options
  • Save kevinburke/ad20587e6694acb9251f7d7e25c77078 to your computer and use it in GitHub Desktop.
Save kevinburke/ad20587e6694acb9251f7d7e25c77078 to your computer and use it in GitHub Desktop.

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: golang/go#12763

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

@symbiat
Copy link

symbiat commented Jan 12, 2018

The Ruby implementation is > 10 lines :-)

@masklinn
Copy link

masklinn 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