flash_extensions 3.1.1 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4a7aafeb611b94c20ec6deecf5e604714e55482
4
- data.tar.gz: b5d57365ddd985af4abebde381662d624b90fb59
3
+ metadata.gz: 29bc0da7ad03da1d52f8eee6a230cf349c977bf7
4
+ data.tar.gz: 4ff32a8160e43520e058d9376bcef76ee0718803
5
5
  SHA512:
6
- metadata.gz: d40db1ddc4db820c4364e1ac382e1c2786d199d0ea4921f007a27cf364ff59bae43fc613277562bbba6d74478d649e8c374ba7a0f6c5b6123d78c7b89ad54d9e
7
- data.tar.gz: 9fd19ad1ee7ba42da595e539fc214b57b98cad4d9a5650f8a57d3267d16e57f842007dbbe28c9e821d4daa45ac7131e3e50978f2a89db004bbf38ef13119464c
6
+ metadata.gz: f29027c054f49c5a80423aabb8c70bdd7f6b223a0b6565fcd4e2df10eba781b0f148106c907c0867923a57cd63acf350c01b604fbf1a8198f268e42848932faa
7
+ data.tar.gz: 1e22b5d041b52897ec998a05ec5edec1c583a85cd8ffd95b43c62e38ee99794f7bfde795165165e3feca097e6951676a834dd2f41493aeacebb10de6291ad904
data/README.md CHANGED
@@ -154,6 +154,14 @@ Use the `several?` method to return if there are several types of an element.
154
154
  [].several? #=> false
155
155
  ```
156
156
 
157
+ ####Standard Deviation:####
158
+ Use the `standard_deviation` method to return the standard deviation of elements of a collection.
159
+
160
+ ```ruby
161
+ [1,2,6].variance #=> 2.6457513110645907
162
+ [].variance #=> nil
163
+ ```
164
+
157
165
  ####Sum:####
158
166
  Use the `sum` method to to return the sum of a collection of numbers.
159
167
 
@@ -180,6 +188,14 @@ Use the `take_last_while` method to return the last number of elements of a coll
180
188
  [].take_last_while(&:odd?) #=> []
181
189
  ```
182
190
 
191
+ ####Variance:####
192
+ Use the `variance` method to return the variance of elements of a collection.
193
+
194
+ ```ruby
195
+ [1,2,6].variance #=> 7
196
+ [].variance #=> nil
197
+ ```
198
+
183
199
  ### HashExtensions
184
200
 
185
201
  ####Except:####
@@ -110,6 +110,11 @@ module Enumerable
110
110
  (found_count > 1) ? true : false
111
111
  end
112
112
 
113
+ def standard_deviation
114
+ return if length < 2
115
+ Math.sqrt(variance)
116
+ end
117
+
113
118
  def take_last(n)
114
119
  array = to_a
115
120
 
@@ -125,4 +130,12 @@ module Enumerable
125
130
  result
126
131
  end
127
132
 
133
+ def variance
134
+ collection_length = length
135
+
136
+ return if collection_length < 1
137
+ sum = inject(0) { |accumulator, value| accumulator + (value - mean) ** 2 }
138
+ sum / (collection_length.to_f - 1)
139
+ end
140
+
128
141
  end
@@ -1,3 +1,3 @@
1
1
  module FlashExtensions
2
- VERSION = "3.1.1"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -158,6 +158,20 @@ describe Enumerable do
158
158
  end
159
159
  end
160
160
 
161
+ describe '#standard_deviation' do
162
+ it 'to be the square root of the #variance' do
163
+ expect([1,2,6].standard_deviation).to eq(2.6457513110645907)
164
+ end
165
+
166
+ it 'to be nil for single element arrays' do
167
+ expect([1].standard_deviation).to be_nil
168
+ end
169
+
170
+ it 'to be nil if empty' do
171
+ expect([].standard_deviation).to be_nil
172
+ end
173
+ end
174
+
161
175
  describe "#sum" do
162
176
  it "to be 0" do
163
177
  expect([].sum).to eq(0)
@@ -192,4 +206,14 @@ describe Enumerable do
192
206
  end
193
207
  end
194
208
 
209
+ describe '#variance' do
210
+ it 'to be the mean squared deviation of the sample (n - 1 denominator)' do
211
+ expect([1,2,6].variance).to eq(7)
212
+ end
213
+
214
+ it 'to be nil if empty' do
215
+ expect([].variance).to be_nil
216
+ end
217
+ end
218
+
195
219
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flash_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-22 00:00:00.000000000 Z
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler