Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add Enumerable#frequency #7

Merged
merged 1 commit into from

2 participants

@fuadsaud

No description provided.

lib/powerpack/enumerable/frequency.rb
@@ -0,0 +1,19 @@
+unless Enumerable.method_defined? :tail
@bbatsov Owner
bbatsov added a note

I sense a copy/paste error here ;-) tail => frequency.

Oh gosh, totally.

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

Shouldn't we name the method frequencies? It's named like this in Clojure and the result is actually several frequencies anyways.

@fuadsaud

Hum, makes sense. I took it from from Haskell, but I agree the plural form is better suited.

README.md
@@ -27,6 +27,7 @@ Or install it yourself as:
* [Enumerable](http://rdoc.info/github/bbatsov/powerpack/Enumerable)
* [#drop_last](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last-instance_method)
* [#drop_last_while](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last_while-instance_method)
+ * [#frequency](http://rdoc.info/github/bbatsov/powerpack/Enumerable#frequency-instance_method)
@bbatsov Owner
bbatsov added a note

It should be frequencies here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bbatsov bbatsov merged commit 9091159 into bbatsov:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 28, 2013
  1. Add Enumerable#frequency

    Fuad Saud authored
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -27,6 +27,7 @@ Or install it yourself as:
* [Enumerable](http://rdoc.info/github/bbatsov/powerpack/Enumerable)
* [#drop_last](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last-instance_method)
* [#drop_last_while](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last_while-instance_method)
+ * [#frequencies](http://rdoc.info/github/bbatsov/powerpack/Enumerable#frequencies-instance_method)
* [#several?](http://rdoc.info/github/bbatsov/powerpack/Enumerable#several?-instance_method)
* [#sproduct](http://rdoc.info/github/bbatsov/powerpack/Enumerable#sproduct?-instance_method)
* [#sum](http://rdoc.info/github/bbatsov/powerpack/Enumerable#sum-instance_method)
View
1  lib/powerpack/enumerable.rb
@@ -1,5 +1,6 @@
require_relative 'enumerable/drop_last'
require_relative 'enumerable/drop_last_while'
+require_relative 'enumerable/frequencies'
require_relative 'enumerable/several'
require_relative 'enumerable/sproduct'
require_relative 'enumerable/sum'
View
19 lib/powerpack/enumerable/frequencies.rb
@@ -0,0 +1,19 @@
+unless Enumerable.method_defined? :frequencies
+ module Enumerable
+ # Counts the number of ocurrence of items in the enumberable.
+ #
+ # @return [Hash] in the format value => count
+ #
+ # @example
+ # [].frequency # => {}
+ # [1, :symbol, 'string', 3, :symbol, 1].frequency
+ # #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }
+ #
+ #
+ def frequencies
+ inject(Hash.new(0)) do |a, e|
+ a.tap { |acc| acc[e] += 1 }
+ end
+ end
+ end
+end
View
20 spec/powerpack/enumerable/frequencies_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe 'Enumerable#frequencies' do
+ context 'empty collection' do
+ it 'evaluates to an empty hash' do
+ expect([].frequencies).to eql({})
+ end
+ end
+
+ context 'populated collection' do
+ it 'counts the number of ocurrences and returns a hash in the form value => count' do
+ expect([1, 1, :symbol, 3, 1, :symbol, 'string'].frequencies).to eql(
+ 1 => 3,
+ 3 => 1,
+ 'string' => 1,
+ :symbol => 2
+ )
+ end
+ end
+end
Something went wrong with that request. Please try again.