Sha256: 12f6ec94a8890094c583cbbe26984dbcf8166f45f23d4bc237a9dd17fc80598d

Contents?: true

Size: 544 Bytes

Versions: 3

Compression:

Stored size: 544 Bytes

Contents

class Array

  unless method_defined?(:product) # 1.8.7+

    # Provides the cartesian product of two or more arrays.
    #
    #   a = []
    #   [1,2].product([4,5])
    #   a  #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
    #
    # CREDIT: Thomas Hafner

    def product(*enums)
      enums.unshift self
      result = [[]]
      while [] != enums
        t, result = result, []
        b, *enums = enums
        t.each do |a|
          b.each do |n|
            result << a + [n]
          end
        end
      end
      result
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/array/product.rb
facets-2.8.3 lib/core/facets/array/product.rb
facets-2.8.2 lib/core/facets/array/product.rb