Use value_counts if tally is not available

This commit is contained in:
kojix2 2021-05-28 10:24:45 +09:00
parent 9e15c0ac2a
commit a4e747969f

View File

@ -11,10 +11,11 @@ module YouPlot
if tally && Enumerable.method_defined?(:tally) if tally && Enumerable.method_defined?(:tally)
arr.tally arr.tally
else else
# https://github.com/marcandre/backports # value_counts Enumerable::Statistics
arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 } arr.value_counts(dropna: false)
.tap { |h| h.default = nil }
end end
# faster than `.sort_by{|a| a[1]}`, `.sort_by(a:last)`
# `.sort{ |a, b| -a[1] <=> -b[1] }
.sort { |a, b| a[1] <=> b[1] } .sort { |a, b| a[1] <=> b[1] }
.reverse .reverse
.transpose .transpose