Name this function:
inject({}) { |h, o| h[yield(o)] = o; h }.values
Hints:
- It’s a variant of a common stdlib function.
- 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.
Well …?
uniq_by?uniq_byis 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.)uniq_by. yes! i swear i thought of that before visiting the comments page.