On Tue, 14 Dec 2004 00:12:50 +0900, trans. (T. Onoma) <transami / runbox.com>
wrote:

> I have this method:
> 
>   def each_unique_pair
>     self.each_with_index{ |a,i|
>       self[(i+1)..-1].each{ |b| yield a,b }
>     }
>   end
> 
> I'm trying to write a generalized version but having a bit of time about it:
> 
>   def each_unique(n=2)
>     # how?
>   end
> 
> My (imperfect) solutions keep leading me to recursion but I'd rather avoid it 
> (as I think it would be less efficient, correct me if I'm wrong)

One way:

  class Array
    def each_combination(k)
      n = self.size 
      return unless (1..n) === k
      idx = (0...k).to_a
      loop do
        yield self.values_at(*idx)
        i  = k - 1
        i -= 1 while idx[i] == n - k + i
        break if i < 0
        idx[i] += 1
        (i + 1 ... k).each {|j| idx[j] = idx[i] + j - i}
      end
    end
  end

  a = %w|a b c d e|
  n = 3
  a.each_combination(3) do |c|
    p c
  end


regards,
andrew