[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [GOLF]: partitioning an array

Daniel Sheppard

12/7/2006 6:08:00 AM

> a.inject({}){|h,v|(h[yield(v)]||=[])<<v;h}.values

It's actually cleaner and shorter without that inject.

def group a
h={}
a.map{|v|(h[yield(v)]||=[])<<v}
h.values
end

and if we're golfing here, it's (slightly) shorter to add that extra arg
and dump that yield:

def group a;h={};a.map{|v|(h[yield(v)]||=[])<<v};h.values;end
def group a,&b;h={};a.map{|v|(h[b[v]]||=[])<<v};h.values;end