class CartesianProduct
include Enumerable
# your code here
end
#Examples of use
c = CartesianProduct.new([:a,:b], [4,5])
c.each { |elt| puts elt.inspect }
# [:a, 4]
# [:a, 5]
# [:b, 4]
# [:b, 5]
c = CartesianProduct.new([:a,:b], [])
c.each { |elt| puts elt.inspect }
# (nothing printed since Cartesian product
# of anything with an empty collection is empty)
I am new to ruby. And I understand how to define a instance method of Cartesian Product, but I have no clue to this. How should I construct the class object to fulfill the requirement.
I wouldn’t use a class for that, but keeping the structure of the question, I’d write:
Instead, I’d simply write
xs.product(ys)or build my ownArray#lazy_productif laziness were important (see this ticket).