YouPlot/lib/youplot/backends/processing.rb
2021-01-19 22:37:15 +09:00

25 lines
589 B
Ruby

# frozen_string_literal: true
module YouPlot
# plotting functions.
module Backends
module Processing
module_function
def count_values(arr, tally: true)
# tally was added in Ruby 2.7
if tally && Enumerable.method_defined?(:tally)
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