Simplify with #sum

This commit is contained in:
Brian Underwood
2017-04-11 14:57:32 -04:00
parent 868ed91672
commit 13aed0b2ae
3 changed files with 5 additions and 4 deletions

View File

@@ -2,9 +2,10 @@ class Array
def entropy
each_with_object(Hash.new(0)) do |i, result|
result[i] += 1
end.values.inject(0) do |sum, count|
end.values.sum do |count|
percentage = count.to_f / length
sum + -percentage * Math.log2(percentage)
-percentage * Math.log2(percentage)
end
end
end