From a4e747969f3dbda5cff1ee4801207acfb6367c34 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Fri, 28 May 2021 10:24:45 +0900 Subject: [PATCH] Use value_counts if tally is not available --- lib/youplot/backends/processing.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/youplot/backends/processing.rb b/lib/youplot/backends/processing.rb index 5ce6a8f..bff0b4e 100644 --- a/lib/youplot/backends/processing.rb +++ b/lib/youplot/backends/processing.rb @@ -11,10 +11,11 @@ module YouPlot 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 } + # value_counts Enumerable::Statistics + arr.value_counts(dropna: false) 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] } .reverse .transpose