Sha256: b7b18063769cc3f9df8dffce4e92f5506d70262205d36bbce29ed86c0f5cdde9

Contents?: true

Size: 746 Bytes

Versions: 2

Compression:

Stored size: 746 Bytes

Contents

module Enumerable

  # As with each_combo but returns combos collected in an array.
  #
  #   CREDIT: Trans

  def combos
    a = []
    each_combo{ |c| a << c }
    a
  end

  # Expected to be an enumeration of arrays. This method
  # iterates through combinations of each in position.
  #
  #   a = [ [0,1], [2,3] ]
  #   a.each_combo { |c| p c }
  #
  # produces
  #
  #   [0, 2]
  #   [0, 3]
  #   [1, 2]
  #   [1, 3]
  #
  #   CREDIT: Trans

  def each_combo
    a = collect{ |x|
      x.respond_to?(:to_a) ? x.to_a : 0..x
    }

    if a.size == 1
      r = a.shift
      r.each{ |n|
        yield n
      }
    else
      r = a.shift
      r.each{ |n|
        a.each_combo{ |s|
          yield [n, *s]
        }
      }
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-2.2.1 lib/core/facets/enumerable/combos.rb
facets-2.3.0 lib/core/facets/enumerable/combos.rb