Sha256: e9616dc9aecb3eb1465d2c9bc6ae662b534fed5d41ed2ee475d1aee947a5938c

Contents?: true

Size: 446 Bytes

Versions: 10

Compression:

Stored size: 446 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

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/modulate.rb
facets-2.8.3 lib/core/facets/enumerable/modulate.rb
facets-2.8.2 lib/core/facets/enumerable/modulate.rb
facets-2.8.1 lib/core/facets/enumerable/modulate.rb
facets-2.8.0 lib/core/facets/enumerable/modulate.rb
facets-2.7.0 lib/core/facets/enumerable/modulate.rb
facets-2.6.0 lib/core/facets/enumerable/modulate.rb
facets-2.5.1 lib/core/facets/enumerable/modulate.rb
facets-2.5.0 lib/core/facets/enumerable/modulate.rb
facets-2.5.2 lib/core/facets/enumerable/modulate.rb