Module: YouPlot::Backends::Processing

Defined in:
lib/youplot/backends/processing.rb

Class Method Summary collapse

Class Method Details

.count_values(arr) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/youplot/backends/processing.rb', line 9

def count_values(arr)
  # tally was added in Ruby 2.7
  if 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