Enumerable#repeated_permutation

The computer I'm using doesn't have a version of Ruby which supports Enumerable#repeated_permutations.

The following snippet was copied from http://pastebin.com/BnHnExBQ [pastebin.com].

module Enumerable
  def repeated_permutation(size, &blk)
    f = proc do |memo, &blk|
      if memo.size == size
        blk.call memo
      else
        self.each do |i|
          f.call memo + [i], &blk
        end
      end
    end

    if block_given?
      f.call [], &blk
    else
      Enumerator.new(f, :call, [])
    end
  end
end

Tags:
Source:
2117hrs.txt
Published:
14-04-2012 21:17