Sha256: 4ceee04235b3d5c9deeefa7488ebfcf8d4417ab966cd3a340d97cee9c2125ea1
Contents?: true
Size: 487 Bytes
Versions: 6
Compression:
Stored size: 487 Bytes
Contents
# frozen_string_literal: true class WeightedList class Normalizer def self.call(collection) new(collection).call end def initialize(collection) @hash = collection.to_h end def call hash.each_with_object({}) do |(item, weight), normalized_hash| normalized_hash[item] = weight.fdiv(total_weight) end end private attr_reader :hash def total_weight @total_weight ||= hash.values.reduce(&:+) end end end
Version data entries
6 entries across 6 versions & 1 rubygems