On Oct 19, 2009, at 3:14 PM, Suraj Kurapati wrote:
>
> Pretty cool.  An improvement would be to return an Enumerator.

How about:


module Enumerable
   def permutations
     return enum_for(:permutations) unless block_given?

     if length < 2
       yield self
     else
       each do |element|
         select { |candidate|
           candidate != element
         }.permutations { |smaller|
           yield [element, *smaller]
         }
       end
     end
   end
end


[1,2,3].permutations { |x| p x }

p [1,2,3].permutations.map { |x| x.reverse }


Gary Wright