module Enumerable
Public Instance Methods
cluster() { |ele| ... }
click to toggle source
rubocop:disable Lint/UnusedMethodArgument
# File lib/active_object/enumerable.rb, line 57 def cluster(&block) each_with_object([]) do |ele, results| last_res = results.last last_res && (yield(ele) == yield(last_res.last)) ? last_res << ele : results << [ele] end end
critical_zscore(identity = nil)
click to toggle source
rubocop:enable Lint/UnusedMethodArgument
# File lib/active_object/enumerable.rb, line 65 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
defference(identity = 0, &block)
click to toggle source
# File lib/active_object/enumerable.rb, line 78 def defference(identity = 0, &block) if block_given? map(&block).defference(identity) else inject { |key, val| key - val } || identity end end
divisible(identity = 0, &block)
click to toggle source
# File lib/active_object/enumerable.rb, line 86 def divisible(identity = 0, &block) if block_given? map(&block).divisible(identity) else inject { |key, val| key / val } || identity end end
drop_last(num)
click to toggle source
# File lib/active_object/enumerable.rb, line 94 def drop_last(num) collection_length = to_a.length return self if num > collection_length self[0...(collection_length - num)] end
drop_last_if() { |val| ... }
click to toggle source
# File lib/active_object/enumerable.rb, line 101 def drop_last_if return to_enum(:drop_last_if) unless block_given? dropping = true result = [] reverse_each { |val| result.unshift(val) unless dropping &&= yield(val) } result end
exactly?(num) { |*opt| ... }
click to toggle source
# File lib/active_object/enumerable.rb, line 110 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)
click to toggle source
# File lib/active_object/enumerable.rb, line 122 def exclude?(object) !include?(object) end
expand()
click to toggle source
# File lib/active_object/enumerable.rb, line 126 def expand map { |val| val.is_a?(Enumerable) ? val.expand : val } end
exponential(identity = 0, &block)
click to toggle source
# File lib/active_object/enumerable.rb, line 130 def exponential(identity = 0, &block) if block_given? map(&block).exponential(identity) else inject { |key, val| key**val } || identity end end
incase?(object)
click to toggle source
rubocop:disable Style/CaseEquality
# File lib/active_object/enumerable.rb, line 139 def incase?(object) any? { |val| object === val } end
interpose(sep, &block)
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/active_object/enumerable.rb, line 145 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?() { |val| ... }
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/active_object/enumerable.rb, line 170 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)
click to toggle source
# File lib/active_object/enumerable.rb, line 183 def mean(identity = 0) return identity if empty? collection_length = length sum.to_f / collection_length.to_f end
Also aliased as: average
median(identity = 0)
click to toggle source
# File lib/active_object/enumerable.rb, line 192 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] return sorted_collection unless (collection_length % 2).zero? (collection_sorted[half_collection - 1.0] + sorted_collection) / 2.0 end
mode(identity = 0)
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/active_object/enumerable.rb, line 205 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)
click to toggle source
rubocop:enable Metrics/AbcSize
# File lib/active_object/enumerable.rb, line 217 def multiple(identity = 0, &block) if block_given? map(&block).multiple(identity) else inject { |key, val| key * val } || identity end end
occurrences()
click to toggle source
# File lib/active_object/enumerable.rb, line 225 def occurrences each_with_object(::Hash.new(0)) { |key, hsh| hsh[key] += 1 } end
percentile(num, identity = 0)
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/active_object/enumerable.rb, line 230 def percentile(num, identity = 0) return identity unless length.positive? collection_sorted = sort index = (num.to_f / 100) * collection_sorted.length - 1 if index != index.to_i collection_sorted[index.ceil] || identity elsif collection_sorted.length.even? sample_one = collection_sorted.at(index) sample_two = collection_sorted.at(index + 1) (sample_one + sample_two) / 2 else collection_sorted[index] || identity end end
range(identity = 0)
click to toggle source
rubocop:enable Metrics/AbcSize
# File lib/active_object/enumerable.rb, line 248 def range(identity = 0) return identity unless length.positive? collection_sorted = sort collection_sorted.last - collection_sorted.first end
reject_outliers()
click to toggle source
# File lib/active_object/enumerable.rb, line 255 def reject_outliers cz = critical_zscore reject { |value| zscore(value) > cz } end
reject_outliers!()
click to toggle source
# File lib/active_object/enumerable.rb, line 261 def reject_outliers! replace(reject_outliers) end
select_outliers()
click to toggle source
# File lib/active_object/enumerable.rb, line 265 def select_outliers cz = critical_zscore select { |value| zscore(value) > cz } end
several?() { |*opt| ... }
click to toggle source
# File lib/active_object/enumerable.rb, line 271 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)
click to toggle source
# File lib/active_object/enumerable.rb, line 283 def standard_deviation(identity = 0) return identity if length < 2 ::Math.sqrt(variance) end
sum(identity = 0, &block)
click to toggle source
# File lib/active_object/enumerable.rb, line 289 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, val| sum + val } || identity end end
take_last(num)
click to toggle source
# File lib/active_object/enumerable.rb, line 297 def take_last(num) collection_length = to_a.length return self if num > collection_length self[(collection_length - num)..-1] end
take_last_if() { |val| ... }
click to toggle source
# File lib/active_object/enumerable.rb, line 304 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)
click to toggle source
# File lib/active_object/enumerable.rb, line 312 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)
click to toggle source
# File lib/active_object/enumerable.rb, line 320 def zscore(value) sd = standard_deviation return 0 if sd.zero? (mean - value).abs / sd end