From 86c0d8ed53ddac4313cbaaf357fd5d462e489b5c Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Thu, 27 May 2021 23:04:23 +0900 Subject: [PATCH] Use enumerable-statistics's value_counts instead of tally --- lib/youplot/backends/processing.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/youplot/backends/processing.rb b/lib/youplot/backends/processing.rb index 5ce6a8f..eb5aea3 100644 --- a/lib/youplot/backends/processing.rb +++ b/lib/youplot/backends/processing.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'enumerable/statistics' + module YouPlot # plotting functions. module Backends @@ -7,17 +9,8 @@ module YouPlot 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 + a = arr.value_counts + [a.keys, a.values] end end end