Avoid polluting namespace

This commit is contained in:
Sam Oliver
2021-09-17 13:12:47 +02:00
parent a58ec254f6
commit f6d316bf1e
7 changed files with 37 additions and 31 deletions

View File

@@ -0,0 +1,13 @@
module ArrayEntropy
refine Array do
def entropy
each_with_object(Hash.new(0)) do |i, result|
result[i] += 1
end.values.inject(0, :+) do |count|
percentage = count.to_f / length
-percentage * Math.log2(percentage)
end
end
end
end