YouPlot/lib/youplot/backends/processing.rb

25 lines
589 B
Ruby
Raw Normal View History

2020-11-25 16:31:41 +08:00
# frozen_string_literal: true
module YouPlot
# plotting functions.
module Backends
module Processing
module_function
2021-01-19 21:37:15 +08:00
def count_values(arr, tally: true)
2020-11-25 16:31:41 +08:00
# tally was added in Ruby 2.7
2021-01-19 21:37:15 +08:00
if tally && Enumerable.method_defined?(:tally)
2020-11-25 16:31:41 +08:00
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