Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

http://ttt.ggnore.net

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Power-Set generating function for Ruby

Extend Ruby's Array class by a method that returns the "power set" with the following code.

   1  
   2  class Array
   3    # Returns the "power set" for this Array. This means that an array with all
   4    # subsets of the array's elements will be returned.
   5    def power
   6      # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
   7      inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
   8    end
   9  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS