I am writing a Multiset in Ruby. The Union, Intersection operations where simple and return a MultiSet. But, I am not sure what the return type of the cartesian product should be, should I return an array of tuples? is that a good api? MultiSet.new([1,1]) * (MultiSet.new([1,2])In Math it is{1,1}*{1,2} = {(1,1),(1,1),(1,2),(1,2)}
What would be the return type of a cartesian product of a multiset
data structures;ruby
I'm not sure why you think the product of two multisets should be an array. It should obviously be a multiset. A multiset of ordered pairs, to be precise.Ruby doesn't have a prebuilt representation of ordered pairs (or more generally tuples), so you could do one of two things:provide your own pair implementationdo what Hash#each does and represent pairs as two-element arraysI believe the second option is the more sensible one, so your return type should be a MultiSet of two-element Arrays.But I'm not convinced of your code example, either. In your example, you are implying that MultiSet.new([1, 1]) * MultiSet.new([1, 2])and {1, 1} * {1, 2} = {(1, 1), (1, 1), (1, 2), (1, 2)}are the same thing, but they are not!MultiSet.new([1, 1]) * MultiSet.new([1, 2])is the same as {[1, 1]} * {[1, 2]} = {([1, 1], [1, 2])}and {1, 1} * {1, 2} = {(1, 1), (1, 1), (1, 2), (1, 2)}is the same as MultiSet.new(1, 1) * MultiSet.new(1, 2) ==   MultiSet.new([1, 1], [1, 1], [1, 2], [1, 2])