Sha256: 83bd85e14e37578ae939be56b06bebba59ee370d33bc8def773aadfc2693bee8

Contents?: true

Size: 448 Bytes

Versions: 6

Compression:

Stored size: 448 Bytes

Contents

module Enumerable

  # Modulate. Divide an array into groups by modulo of the index.
  #
  #   [2,4,6,8].modulate(2)  #=> [[2,6],[4,8]]
  #
  # CREDIT: Trans
  #
  # NOTE: Would the better name for this be 'collate'?

  def modulate(modulo)
    return to_a if modulo == 1
    raise ArgumentError, 'bad modulo' if size % modulo != 0
    r = Array.new(modulo, [])
    (0...size).each do |i|
      r[i % modulo] += [self[i]]
    end
    r
  end

end

Version data entries

6 entries across 5 versions & 1 rubygems

Version Path
facets-2.9.2 lib/core/facets/enumerable/modulate.rb
facets-2.9.2 src/core/facets/enumerable/modulate.rb
facets-2.9.1 lib/core/facets/enumerable/modulate.rb
facets-2.9.0 lib/core/facets/enumerable/modulate.rb
facets-2.9.0.pre.2 lib/core/facets/enumerable/modulate.rb
facets-2.9.0.pre.1 lib/core/facets/enumerable/modulate.rb