Module: Enumerable
- Defined in:
- lib/active_object/enumerable.rb
Instance Method Summary collapse
-
#cluster(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#critical_zscore(identity = nil) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
- #difference(identity = 0, &block) ⇒ Object
- #divisible(identity = 0, &block) ⇒ Object
- #drop_last(num) ⇒ Object
- #drop_last_if ⇒ Object
- #exactly?(num) ⇒ Boolean
- #exclude?(object) ⇒ Boolean
- #expand ⇒ Object
- #exponential(identity = 0, &block) ⇒ Object
-
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality.
-
#interpose(sep, &block) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#many? ⇒ Boolean
rubocop:enable Metrics/MethodLength.
- #mean(identity = 0) ⇒ Object (also: #average)
- #median(identity = 0) ⇒ Object
-
#mode(identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#multiple(identity = 0, &block) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #occurrences ⇒ Object
-
#percentile(num, identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#range(identity = 0) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #reject_outliers ⇒ Object
- #reject_outliers! ⇒ Object
- #select_outliers ⇒ Object
- #several? ⇒ Boolean
- #standard_deviation(identity = 0) ⇒ Object
- #sum(identity = 0, &block) ⇒ Object
- #take_last(num) ⇒ Object
- #take_last_if ⇒ Object
- #variance(identity = 0) ⇒ Object
- #zscore(value) ⇒ Object
Instance Method Details
#cluster(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
57 58 59 60 61 62 63 64 |
# File 'lib/active_object/enumerable.rb', line 57 def cluster(&block) result = [] each do |ele| last_res = result.last last_res && (yield(ele) == yield(last_res.last)) ? last_res << ele : result << [ele] end result end |
#critical_zscore(identity = nil) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_object/enumerable.rb', line 67 def critical_zscore(identity = nil) collection_length = length result = nil CRITICAL_ZSCORE.keys.sort.each do |key| break if key > collection_length result = CRITICAL_ZSCORE[key] end result || identity end |
#difference(identity = 0, &block) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/active_object/enumerable.rb', line 80 def difference(identity = 0, &block) if block_given? map(&block).difference(identity) else inject { |key, val| key - val } || identity end end |
#divisible(identity = 0, &block) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/active_object/enumerable.rb', line 88 def divisible(identity = 0, &block) if block_given? map(&block).divisible(identity) else inject { |key, val| key / val } || identity end end |
#drop_last(num) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/active_object/enumerable.rb', line 96 def drop_last(num) collection_length = to_a.length return self if num > collection_length self[0...(collection_length - num)] end |
#drop_last_if ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_object/enumerable.rb', line 103 def drop_last_if return(to_enum(:drop_last_if)) unless block_given? result = [] dropping = true reverse_each do |val| result.unshift(val) unless dropping &&= yield(val) end result end |
#exactly?(num) ⇒ Boolean
114 115 116 117 118 119 120 121 122 |
# File 'lib/active_object/enumerable.rb', line 114 def exactly?(num) found_count = 0 if block_given? each { |*opt| found_count += 1 if yield(*opt) } else each { |opt| found_count += 1 if opt } end found_count > num ? false : num == found_count end |
#exclude?(object) ⇒ Boolean
124 125 126 |
# File 'lib/active_object/enumerable.rb', line 124 def exclude?(object) !include?(object) end |
#expand ⇒ Object
128 129 130 |
# File 'lib/active_object/enumerable.rb', line 128 def map { |val| val.is_a?(Enumerable) ? val. : val } end |
#exponential(identity = 0, &block) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/active_object/enumerable.rb', line 132 def exponential(identity = 0, &block) if block_given? map(&block).exponential(identity) else inject { |key, val| key**val } || identity end end |
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality
141 142 143 |
# File 'lib/active_object/enumerable.rb', line 141 def incase?(object) any? { |val| object === val } end |
#interpose(sep, &block) ⇒ Object
rubocop:disable Metrics/MethodLength
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_object/enumerable.rb', line 147 def interpose(sep, &block) enum = Enumerator.new do |val| items = each loop do begin val << items.next rescue StopIteration break end begin items.peek rescue StopIteration break else val << sep end end end block ? enum.each(&block) : enum end |
#many? ⇒ Boolean
rubocop:enable Metrics/MethodLength
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/active_object/enumerable.rb', line 172 def many? found_count = 0 if block_given? any? do |val| found_count += 1 if yield val found_count > 1 end else any? { (found_count += 1) > 1 } end end |
#mean(identity = 0) ⇒ Object Also known as: average
184 185 186 187 188 189 |
# File 'lib/active_object/enumerable.rb', line 184 def mean(identity = 0) return identity if empty? collection_length = length sum.to_f / collection_length.to_f end |
#median(identity = 0) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/active_object/enumerable.rb', line 193 def median(identity = 0) collection_length = length.to_f collection_sorted = sort return(identity) unless collection_length > 0.0 half_collection = collection_length / 2.0 sorted_collection = collection_sorted[half_collection] if (collection_length % 2).zero? (collection_sorted[half_collection - 1.0] + sorted_collection) / 2.0 else sorted_collection end end |
#mode(identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/active_object/enumerable.rb', line 210 def mode(identity = 0) return identity unless length.positive? frequency_distribution = each_with_object(::Hash.new(0)) { |val, hsh| hsh[val] += 1 } frequency_top_two = frequency_distribution.sort_by { |_, val| -val }.take(2) top_two_first = frequency_top_two.first return if frequency_top_two.length != 1 && top_two_first.last == frequency_top_two.last.last top_two_first.first end |
#multiple(identity = 0, &block) ⇒ Object
rubocop:enable Metrics/AbcSize
223 224 225 226 227 228 229 |
# File 'lib/active_object/enumerable.rb', line 223 def multiple(identity = 0, &block) if block_given? map(&block).multiple(identity) else inject { |key, val| key * val } || identity end end |
#occurrences ⇒ Object
231 232 233 |
# File 'lib/active_object/enumerable.rb', line 231 def occurrences each_with_object(::Hash.new(0)) { |key, hsh| hsh[key] += 1 } end |
#percentile(num, identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/active_object/enumerable.rb', line 236 def percentile(num, identity = 0) return identity unless length.positive? collection_sorted = sort rank = (num.to_f / 100) * (length + 1) if rank.fraction? truncated_rank = rank.truncate sample_one = collection_sorted[truncated_rank - 1] sample_two = collection_sorted[truncated_rank] (rank.fraction * (sample_two - sample_one)) + sample_one else collection_sorted[rank - 1] end end |
#range(identity = 0) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
254 255 256 257 258 259 |
# File 'lib/active_object/enumerable.rb', line 254 def range(identity = 0) return identity unless length.positive? collection_sorted = sort collection_sorted.last - collection_sorted.first end |
#reject_outliers ⇒ Object
261 262 263 264 |
# File 'lib/active_object/enumerable.rb', line 261 def reject_outliers cz = critical_zscore reject { |value| zscore(value) > cz } end |
#reject_outliers! ⇒ Object
266 267 268 |
# File 'lib/active_object/enumerable.rb', line 266 def reject_outliers! replace(reject_outliers) end |
#select_outliers ⇒ Object
270 271 272 273 |
# File 'lib/active_object/enumerable.rb', line 270 def select_outliers cz = critical_zscore select { |value| zscore(value) > cz } end |
#several? ⇒ Boolean
275 276 277 278 279 280 281 282 283 |
# File 'lib/active_object/enumerable.rb', line 275 def several? found_count = 0 if block_given? each { |*opt| found_count += 1 if yield(*opt) } else each { |opt| found_count += 1 if opt } end found_count > 1 end |
#standard_deviation(identity = 0) ⇒ Object
285 286 287 288 289 |
# File 'lib/active_object/enumerable.rb', line 285 def standard_deviation(identity = 0) return identity if length < 2 ::Math.sqrt(variance) end |
#sum(identity = 0, &block) ⇒ Object
291 292 293 294 295 296 297 |
# File 'lib/active_object/enumerable.rb', line 291 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, val| sum + val } || identity end end |
#take_last(num) ⇒ Object
299 300 301 302 303 304 305 |
# File 'lib/active_object/enumerable.rb', line 299 def take_last(num) collection_length = to_a.length return self if num > collection_length self[(collection_length - num)..-1] end |
#take_last_if ⇒ Object
307 308 309 310 311 312 313 |
# File 'lib/active_object/enumerable.rb', line 307 def take_last_if return(to_enum(:take_last_if)) unless block_given? result = [] reverse_each { |val| yield(val) ? result.unshift(val) : break } result end |
#variance(identity = 0) ⇒ Object
315 316 317 318 319 320 321 322 |
# File 'lib/active_object/enumerable.rb', line 315 def variance(identity = 0) collection_length = length return identity if collection_length <= 1 total = inject(0.0) { |sum, val| sum + (val - mean)**2.0 } total.to_f / (collection_length.to_f - 1.0) end |
#zscore(value) ⇒ Object
324 325 326 327 328 329 |
# File 'lib/active_object/enumerable.rb', line 324 def zscore(value) sd = standard_deviation return 0 if sd.zero? (mean - value).abs / sd end |