Sha256: 48b6fce2bdc08004e3875a28f6e8537c4d83fec46c3027226b61adee792a2119

Contents?: true

Size: 1.79 KB

Versions: 152

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

# Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block
# and the values are arrays of elements in the collection that correspond to the key.
Puppet::Functions.create_function(:group_by) do
  # @param collection A collection of things to group.
  # @example Group array of strings by length, results in e.g. { 1 => [a, b], 2 => [ab] }
  #   [a, b, ab].group_by |$s| { $s.length }
  # @example Group array of strings by length and index, results in e.g. {1 => ['a'], 2 => ['b', 'ab']}
  #   [a, b, ab].group_by |$i, $s| { $i%2 + $s.length }
  # @example Group hash iterating by key-value pair, results in e.g. { 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }
  #   { a => [1, 2], b => [1] }.group_by |$kv| { $kv[1].length }
  # @example Group hash iterating by key and value, results in e.g. { 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }
  #   { a => [1, 2], b => [1] }.group_by |$k, $v| { $v.length }
  dispatch :group_by_1 do
    required_param 'Collection', :collection
    block_param 'Callable[1,1]', :block
    return_type 'Hash'
  end

  dispatch :group_by_2a do
    required_param 'Array', :array
    block_param 'Callable[2,2]', :block
    return_type 'Hash'
  end

  dispatch :group_by_2 do
    required_param 'Collection', :collection
    block_param 'Callable[2,2]', :block
    return_type 'Hash'
  end

  def group_by_1(collection)
    collection.group_by do |item|
      yield(item)
    end.freeze
  end

  def group_by_2a(array)
    grouped = array.size.times.zip(array).group_by do |k, v|
      yield(k, v)
    end

    grouped.each_with_object({}) do |(k, v), hsh|
      hsh[k] = v.map { |item| item[1] }
    end.freeze
  end

  def group_by_2(collection)
    collection.group_by do |k, v|
      yield(k, v)
    end.freeze
  end
end

Version data entries

152 entries across 152 versions & 1 rubygems

Version Path
puppet-6.16.0 lib/puppet/functions/group_by.rb
puppet-6.16.0-x86-mingw32 lib/puppet/functions/group_by.rb
puppet-6.16.0-x64-mingw32 lib/puppet/functions/group_by.rb
puppet-6.16.0-universal-darwin lib/puppet/functions/group_by.rb
puppet-6.15.0 lib/puppet/functions/group_by.rb
puppet-6.15.0-x86-mingw32 lib/puppet/functions/group_by.rb
puppet-6.15.0-x64-mingw32 lib/puppet/functions/group_by.rb
puppet-6.15.0-universal-darwin lib/puppet/functions/group_by.rb
puppet-6.14.0 lib/puppet/functions/group_by.rb
puppet-6.14.0-x86-mingw32 lib/puppet/functions/group_by.rb
puppet-6.14.0-x64-mingw32 lib/puppet/functions/group_by.rb
puppet-6.14.0-universal-darwin lib/puppet/functions/group_by.rb
puppet-6.13.0 lib/puppet/functions/group_by.rb
puppet-6.13.0-x86-mingw32 lib/puppet/functions/group_by.rb
puppet-6.13.0-x64-mingw32 lib/puppet/functions/group_by.rb
puppet-6.13.0-universal-darwin lib/puppet/functions/group_by.rb
puppet-6.12.0 lib/puppet/functions/group_by.rb
puppet-6.12.0-x86-mingw32 lib/puppet/functions/group_by.rb
puppet-6.12.0-x64-mingw32 lib/puppet/functions/group_by.rb
puppet-6.4.5 lib/puppet/functions/group_by.rb