Sha256: 4cf9611ad2af921ee2536ab362258e655c49dec03d38a56fed67a0f58a93010e

Contents?: true

Size: 1.53 KB

Versions: 109

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

class ReeArray::InGroups
  include Ree::FnDSL

  fn :in_groups

  doc(<<~DOC)
    Splits or iterates over the array in +number+ of groups, padding any
    remaining slots with +fill_with+ unless it is +false+.
    
      in_groups(%w(1 2 3 4 5 6 7 8 9 10), 3, fill_with: nil) {|group| p group}
      ["1", "2", "3", "4"]
      ["5", "6", "7", nil]
      ["8", "9", "10", nil]
    
      in_groups(%w(1 2 3 4 5 6 7 8 9 10, 3, fill_with: '&nbsp;') {|group| p group}
      ["1", "2", "3", "4"]
      ["5", "6", "7", "&nbsp;"]
      ["8", "9", "10", "&nbsp;"]
    
      in_groups(%w(1 2 3 4 5 6 7), 3) {|group| p group}
      ["1", "2", "3"]
      ["4", "5"]
      ["6", "7"]
  DOC
  contract(
    ArrayOf[Any],
    Integer,
    Ksplat[fill_with?: Any],
    Optblock => Or[ArrayOf[Any], Any]
  )
  def call(array, number, **opts, &block)
    # size.div number gives minor group size;
    # size % number gives how many objects need extra accommodation;
    # each group hold either division or division + 1 items.
    division = array.size.div(number)
    modulo = array.size % number

    # create a new array avoiding dup
    groups = []
    start = 0

    number.times do |index|
      length = division + (modulo > 0 && modulo > index ? 1 : 0)
      groups << last_group = array.slice(start, length)

      if opts.has_key?(:fill_with) && modulo > 0 && length == division
        last_group << opts[:fill_with]
      end

      start += length
    end

    if block_given?
      groups.each(&block)
    else
      groups
    end
  end
end

Version data entries

109 entries across 109 versions & 1 rubygems

Version Path
ree_lib-1.0.109 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.104 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.93 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.92 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.91 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb
ree_lib-1.0.90 lib/ree_lib/packages/ree_array/package/ree_array/functions/in_groups.rb