Skip to content

Instantly share code, notes, and snippets.

@mrc
Created May 7, 2011 22:39
Show Gist options
  • Save mrc/960919 to your computer and use it in GitHub Desktop.
Save mrc/960919 to your computer and use it in GitHub Desktop.
Histogram (tweeted by mfeathers)
# module is awesome!
module Enumerable;
def histogram;
groups = group_by {|i| i};
(0..max).map {|bin| (groups[bin] || []).count}
end
end
@mrc
Copy link
Author

mrc commented May 7, 2011

Ruby "module" extends all Enumerable class objects with a histogram function. Gosh!

> [3, 5, 5, 6, 6, 6, 7].histogram
 => [0, 0, 0, 1, 0, 2, 3, 1]

@mrc
Copy link
Author

mrc commented May 7, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment