Skip to content

Instantly share code, notes, and snippets.

@richievos
Created April 17, 2012 22:43
Show Gist options
  • Save richievos/2409583 to your computer and use it in GitHub Desktop.
Save richievos/2409583 to your computer and use it in GitHub Desktop.
Enumerable.class_eval do
def dump_spark_histogram
element_count_pairs = self.group_by { |x| x }.to_a.map { |el, els| [el, els.size] }
element_count_pairs = element_count_pairs.sort_by(&:first)
max_el_len = element_count_pairs.map(&:first).map(&:to_s).map(&:size).max
element_count_pairs.each do |el, count|
puts("%#{max_el_len}s: #{('=' * count)}" % el)
end
end
end
array = ['a', 'b', 'c', 'd', 'a', 'a', 'c', 'dd', 'eee', 'eee', 'ffff', 'a']
array.dump_spark_histogram
# outputs
# a: ====
# b: =
# c: ==
# d: =
# dd: =
# eee: ==
# ffff: =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment