Sha256: 0ad3388d1162f489f48c44a0a5f42b63a7fda7532160ea43f485b509221bfe3a

Contents?: true

Size: 834 Bytes

Versions: 7

Compression:

Stored size: 834 Bytes

Contents

class Array

  unless method_defined?(:combination) # 1.8.7+

    require 'facets/enumerator'

    # Yields the block to each unique combination of _n_ elements.
    #
    #   a = %w|a b c d|
    #   a.combination(3)
    #
    # produces
    #
    #   [["a", "b", "c"],
    #    ["a", "b", "d"],
    #    ["a", "c", "d"],
    #    ["b", "c", "d"]]
    #
    # CREDIT: Florian Gross

    def combination(k=2)
      if block_given?
        s = to_a
        n = s.size
        return unless (1..n) === k
        idx = (0...k).to_a
        loop do
          yield s.values_at(*idx)
          i = k - 1
          i -= 1 while idx[i] == n - k + i
          break if i < 0
          idx[i] += 1
          (i + 1 ... k).each {|j| idx[j] = idx[i] + j - i}
        end
      else
        to_enum(:combination, k)
      end
    end

  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/combination.rb
facets-2.9.2 lib/core/facets/array/combination.rb
facets-2.9.2 src/core/facets/array/combination.rb
facets-2.9.1 lib/core/facets/array/combination.rb
facets-2.9.0 lib/core/facets/array/combination.rb
facets-2.9.0.pre.2 lib/core/facets/array/combination.rb
facets-2.9.0.pre.1 lib/core/facets/array/combination.rb