From: William James Date: 2007-02-03T10:35:10+09:00 Subject: Re: cartesian product of arrays On Feb 2, 4:04 pm, Thomas Hafner wrote: > "William James" wrote/schrieb <1170361559.491476.96...@p10g2000cwp.googlegroups.com>: > > > Here's a shorter but slower way. > > > def cart_prod( *args ) > > args.inject([[]]){|old,lst| > > lst.inject([]){|new,e| > > new + old.map{|c| c.dup << e}}} > > end > > Wow, it's really short. Maybe it's not slower at all. Did you just > that? Or did you compare it actually? > > Regards > Thomas I tested it; it's slower. This one isn't quite as slow. def cart_prod( *args ) args.inject([[]]){|old,lst| new = [] lst.each{|e| new += old.map{|c| c.dup << e }} new } end