Sha256: ceb15e6f664a9448026051b7f4d43b2c42c31ce0f4cb3e033efdb0b581450e8b

Contents?: true

Size: 466 Bytes

Versions: 18

Compression:

Stored size: 466 Bytes

Contents

require 'set'

class Set
  def power_set
    if empty?
      Set[ self ]
    else
      any_value = [to_a.first]
      subsubs = (self - any_value).power_set
      subsubs + subsubs.map{ |subset| subset + any_value }
    end
  end
end

class Array
  def power_set
    if empty?
      [self]
    else
      subset  = dup
      value   = [ subset.pop ]
      subsubs = subset.power_set
      subsubs.concat( subsubs.map{ |subset| subset + value } )
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
facets-2.8.4 lib/more/facets/set.rb
facets-2.8.3 lib/more/facets/set.rb
facets-2.8.2 lib/more/facets/set.rb
facets-2.8.1 lib/more/facets/set.rb
facets-2.8.0 lib/more/facets/set.rb
facets-2.7.0 lib/more/facets/set.rb
facets-2.6.0 lib/lore/facets/set.rb
facets-2.4.0 lib/facets/set.rb
facets-2.3.0 lib/more/facets/set.rb
facets-2.4.1 lib/facets/set.rb
facets-2.4.3 lib/lore/facets/set.rb
facets-2.4.4 lib/lore/facets/set.rb
facets-2.4.2 lib/lore/facets/set.rb
facets-2.4.5 lib/lore/facets/set.rb
facets-2.5.0 lib/lore/facets/set.rb
facets-2.5.1 lib/lore/facets/set.rb
facets-2.5.2 lib/lore/facets/set.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/lore/facets/set.rb