On Thu, 11 Oct 2001, Guy N. Hurst wrote:

> 
> Luigi Ballabio wrote:
> > ...
> > How about
> >  >>> l = [(x,y) for x in range(1,5) for y in range(1,5) if x+y < 5]
> >  >>> l
> > [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (3, 1)]
> > 
> 
> irb(main):006:0> l=[]; (1..5).each{|x| (1..5).each{|y| l<<[x,y] if x+y<5}}
> 1..5
> irb(main):007:0> l
> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [3, 1]]
> irb(main):008:0> 
> 
Might it be useful with Range#*?

  class Range
    def *(aRangeOrArray)
      a = []
      self.each {|e| aRangeOrArray.to_a.each {|oe| a << [e,oe]}}
      a
    end
  end

  p ((1..5) * (1..5)).select {|x,y| x+y < 5} # => [[1, 1], [1, 2], [1, 3],
                                                   [2, 1], [2, 2], [3, 1]]

or even extending Array#*?

  class Array
    def *(a)
      if a.respond_to?(:to_a)
        cart_prod = []
        self.each {|e| a.to_a.each {|oe| cart_prod << [e,oe]}}
        cart_prod
      else
        super(a)
      end
    end
  end

  p ([1,2,3] * (1..5)).select {|x,y| x+y < 5} # => [[1, 1], [1, 2], 
                                                    [1, 3], [2, 1], 
                                                    [2, 2], [3, 1]]

Maybe not for std Ruby; probably rare and not efficient (generate then
filter).

Regards,

/Robert