"Mark Wilson" <mwilson13 / cox.net> schrieb im Newsbeitrag
news:40411F81-918A-11D7-BBC7-000393876156 / cox.net...
> The following is a proposed implementation of a cartesian product
> method for the set class. Comments and suggestions would be welcome.
>
> require 'set'
> require 'tuple'
>
> class Set
>    def cproduct(set)
>      set3 = Set.new
>      self.each do |ae|
>        set.each do |be|
>          set3.add(Tuple.new(ae, be))

I'd prefer another method name here, since Tuple.new suggests that there
is a new instance of class Tuple but not Array (as your comment suggests).

>        end
>      end
>      set3
>    end
> end

Did you consider implementing a cartesian product for n Enumerables?  This
could look like

module Enumerable
  def Enumerable.cproduct(*enums)
    enums.each {|e| raise "illegal type" unless e.kind_of? Enumerable
    # impl here
  end
end

OTOH: these beasts are very likely to grow large and thus it might not be
useful to have an implementation for an arbitrary cartesian product at
hand.  Maybe it is more reasonable to create an extra class for this that
creates the cproduct on the fly (i.e. while doing each).

> tuple.rb is from the Seattle Ruby Brigade's Ruby Audit, which can be
> downloaded here:
>
> http://sourceforge.net/project/showfiles.php?group_id=50485
>
> The Tuple class creates immutable arrays. Methods that would modify the
> object in place return an error for attempted modification of a frozen
> object. A deepfreeze method is also added to Enumerable so that the
> Tuple object can't be unfrozen.

*shudder*  :-))

Kind regards

    robert