class HashMath::Matrix

A Matrix allows you to build up a hash of key and values, then it will generate the product of all values.

Attributes

pairs_by_key[R]

Public Class Methods

new() click to toggle source
# File lib/hash_math/matrix.rb, line 21
def initialize
  @pairs_by_key = {}

  freeze
end

Public Instance Methods

add(key, val) click to toggle source
# File lib/hash_math/matrix.rb, line 31
def add(key, val)
  tap { kvp(key).add(val) }
end
add_each(key, vals) click to toggle source
# File lib/hash_math/matrix.rb, line 27
def add_each(key, vals)
  tap { kvp(key).add_each(vals) }
end

Private Instance Methods

kvp(key) click to toggle source
# File lib/hash_math/matrix.rb, line 39
def kvp(key)
  pairs_by_key[key] ||= KeyValuePair.new(key)
end
make_pair_groups() click to toggle source
# File lib/hash_math/matrix.rb, line 43
def make_pair_groups
  pairs_by_key.values.map(&:pairs)
end
pair_products() click to toggle source
# File lib/hash_math/matrix.rb, line 47
def pair_products
  pair_groups = make_pair_groups

  products = pair_groups.inject(pair_groups.shift) { |memo, f| memo.product(f) }
                        &.map { |f| f.is_a?(KeyValuePair::Pair) ? [f] : f.flatten } || []

  products.map { |pairs| recombine(pairs) }
end
recombine(pairs) click to toggle source
# File lib/hash_math/matrix.rb, line 56
def recombine(pairs)
  pairs.each_with_object({}) { |p, memo| memo[p.key] = p.value }
end