Sha256: 4fdf7e4d38fe4b4284c8137072bbc90bcbd5fb04ce05c2644e0391aa69df3aac

Contents?: true

Size: 707 Bytes

Versions: 3

Compression:

Stored size: 707 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

  # Operator alias for cross-product.
  #
  #   a = [1,2] ** [4,5]
  #   a  #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
  #
  # CREDIT: Trans

  alias_method :**, :product

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.1 lib/core/facets/array/product.rb
facets-2.8.0 lib/core/facets/array/product.rb
facets-2.7.0 lib/core/facets/array/product.rb