Skip to content

Instantly share code, notes, and snippets.

@deepfryed
Created February 15, 2012 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepfryed/1832891 to your computer and use it in GitHub Desktop.
Save deepfryed/1832891 to your computer and use it in GitHub Desktop.
unique { f(n) | n ϵ N }
module Enumerable
def uniq_by &block
seen = {}
reject {|value| seen.key?(block.call(value)) ? true : seen[block.call(value)] = false}
end
end
@deepfryed
Copy link
Author

the following does the same but uses more memory depending on dupes

list.group_by(&:method).values.map(&:first)

@deepfryed
Copy link
Author

just so you know, you can do the following only for arrays

list.uniq(&:method)

yeah, dan schooled me - you can gloat dan

@tigris
Copy link

tigris commented Feb 15, 2012

Where's my credit, noob :P

@deepfryed
Copy link
Author

well i need find a good optometrist for dan /O-O/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment