mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-07 23:51:12 +08:00
Use enumerable-statistics's value_counts instead of tally
This commit is contained in:
parent
29dfe424e7
commit
86c0d8ed53
@ -1,5 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'enumerable/statistics'
|
||||||
|
|
||||||
module YouPlot
|
module YouPlot
|
||||||
# plotting functions.
|
# plotting functions.
|
||||||
module Backends
|
module Backends
|
||||||
@ -7,17 +9,8 @@ module YouPlot
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
def count_values(arr, tally: true)
|
def count_values(arr, tally: true)
|
||||||
# tally was added in Ruby 2.7
|
a = arr.value_counts
|
||||||
if tally && Enumerable.method_defined?(:tally)
|
[a.keys, a.values]
|
||||||
arr.tally
|
|
||||||
else
|
|
||||||
# https://github.com/marcandre/backports
|
|
||||||
arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }
|
|
||||||
.tap { |h| h.default = nil }
|
|
||||||
end
|
|
||||||
.sort { |a, b| a[1] <=> b[1] }
|
|
||||||
.reverse
|
|
||||||
.transpose
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user