Feature #10552

[PATCH] Add Enumerable#frequencies and Enumerable#relative_frequencies

Added by brianhempel (Brian Hempel) almost 6 years ago. Updated almost 6 years ago.

Target version:


Counting how many times a value appears in some collection has always been a bit clumsy in Ruby. While Ruby has enough constructs to do it in one line, it still requires knowing the folklore of the optimum solution as well as some acrobatic typing:

%w[cat bird bird horse].each_with_object( { |word, hash| hash[word] += 1 }
# => {"cat" => 1, "bird" => 2, "horse" => 1}

What if Ruby could count for us? This patch adds two methods to enumerables:

%w[cat bird bird horse].frequencies
# => {"bird" => 2, "horse" => 1, "cat" => 1}

%w[cat bird bird horse].relative_frequencies
# => {"bird" => 0.5, "horse" => 0.25, "cat" => 0.25}

To make programmers happier, the returned hash has the most common values first. This is nice because, for example, finding the most common element of a collection becomes trivial:

most_common, count = %w[cat bird bird horse].frequencies.first

Whereas the best you can do with vanilla Ruby is:

most_common, count = %w[cat bird bird horse].each_with_object( { |word, hash| hash[word] += 1 }.max_by(&:last)

# or...

most_common, count = %w[cat bird bird horse].group_by(&:to_s).map { |word, arr| [word, arr.size] }.max_by(&:last)

While I don't like the long method names, "frequencies" and "relative frequencies" are the terms used in basic statistics.


add_enum_frequencies.patch (5.81 KB) add_enum_frequencies.patch brianhempel (Brian Hempel), 11/27/2014 07:55 AM

Related issues

Related to Ruby master - Feature #9970: Add `Hash#map_keys` and `Hash#map_values`Opennobu (Nobuyoshi Nakada)Actions
Related to Ruby master - Feature #7793: New methods on HashAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #10228: Statistics moduleFeedback09/11/2014Actions

Also available in: Atom PDF