Module: HashOp::Math

Defined in:
lib/hash_op/math.rb

Class Method Summary collapse

Class Method Details

.deep_max(hashes, path) ⇒ Object

Parameters:

  • hashes (Array)

    array of Hash

  • path (String, Symbol)

    to deep value in each hash



79
80
81
# File 'lib/hash_op/math.rb', line 79

def deep_max(hashes, path)
  hashes.map { |hash| HashOp::Deep.fetch hash, path }.max
end

.deep_min(hashes, path) ⇒ Object

Parameters:

  • hashes (Array)

    array of Hash

  • path (String, Symbol)

    to deep value in each hash



72
73
74
# File 'lib/hash_op/math.rb', line 72

def deep_min(hashes, path)
  hashes.map { |hash| HashOp::Deep.fetch hash, path }.min
end

.sum_at_path(hashes, path, zero = 0) ⇒ Object

Sum values for the specified hashes at the specified path. The values are added to the specified zero (defaults to numeric 0), and nil values will be coerced to the zero too.

Parameters:

  • hashes  (Array)
  • path (String)
  • zero (Object) (defaults to: 0)

    defaults to [Numeric] 0



62
63
64
65
66
67
# File 'lib/hash_op/math.rb', line 62

def sum_at_path(hashes, path, zero = 0)
  hashes.inject(zero) do |sum, hash|
    value = HashOp::Deep.fetch(hash, path) || zero
    sum + value
  end
end

.sum_on_groups(hashes, grouping_paths, value_paths) ⇒ Array

Sum values in an array of hashes by grouping on a given key.

Example:

hashes = [
  { group_1: :a, group_2: :a, value_1: 1, value_2: 1 },
  { group_1: :a, group_2: :b, value_1: 1, value_2: 2 },
  { group_1: :a, group_2: :b, value_1: 1, value_2: 2 },
  { group_1: :b, group_2: :c, value_1: 1, value_2: 3 }
]
HashOp::Math.sum_on_groups(hashes,
  [:group_1], [:value_1, :value_2]
)
=> [
  { group_1: :a, value_1: 3, value_2: 5 },
  { group_1: :b, value_1: 1, value_2: 3 }
]
HashOp::Math.sum_on_groups(hashes,
  [:group_1, :group_2], [:value_1, :value_2]
)
=> [
  { group_1: :a, group_2: :a, value_1: 1, value_2: 1 },
  { group_1: :a, group_2: :b, value_1: 2, value_2: 4 },
  { group_1: :b, group_2: :c, value_1: 1, value_2: 3 }
]

Parameters:

  • hashes (Array)

    the hashes to be summed

  • group_key (Object)

    the key to use to group items on

  • value_key (Object)

    the key of the values to sum

Returns:

  • (Array)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hash_op/math.rb', line 40

def sum_on_groups(hashes, grouping_paths, value_paths)
    grouped_hashes = Grouping.group_on_paths(hashes, grouping_paths)
    group_paths = Deep.paths(grouped_hashes)
    result = group_paths.map do |group_path|
      group_hashes = HashOp::Deep.fetch(grouped_hashes, group_path)
      group_values = value_paths.map do |value_path|
        group_value = HashOp::Math.sum_at_path(group_hashes, value_path)
        { value_path => group_value }
      end
      Hash[[grouping_paths, group_path].transpose].merge(Merge.flat(group_values))
    end
end