a ruby puzzle

Name this function:
  inject({}) { |h, o| h[yield(o)] = o; h }.values


Hints:

1. It's a variant of a common stdlib function.
2. The name has 7 characters, one of which is an underscore.

A survey of my rubyist colleagues suggests this is a hard question. Much
harder than writing the function given the name, which took about 10
seconds.

4 comments:

Anonymous said...

Well ...?

grant said...

uniq_by?

William said...

uniq_by is correct! Very nice. Of course it doesn't preserve the order of things, which Ruby's uniq does. (But whether that's a feature of uniq or just a happy coincidence that might change in the future, I don't know.)

Brendan said...

uniq_by. yes! i swear i thought of that before visiting the comments page.

Blog Archive