Sha256: 09b4fcf3d50e2d6f2fecf2e78596fa111bae82eb4a8eb178b7408c6cf4c461a8

Contents?: true

Size: 550 Bytes

Versions: 4

Compression:

Stored size: 550 Bytes

Contents

require 'set'

class Set

  #
  # @author Phrogz
  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

  # TODO: Move to Array extensions.

  #
  # @author Phrogz
  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

4 entries across 4 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/standard/facets/set.rb
facets-3.1.0 lib/standard/facets/set.rb
facets-3.0.0 lib/standard/facets/set.rb
facets-2.9.3 lib/standard/facets/set.rb