module Enumerable

Public Instance Methods

difference(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 9
def difference(identity=0)
  inject(:-) || identity
end
divisible(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 13
def divisible(identity=0)
  inject(:/) || identity
end
drop_last(n) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 17
def drop_last(n)
  array = to_a

  return(array) if n > array.size
  array[0...(array.size - n)]
end
drop_last_while() { |value| ... } click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 24
def drop_last_while
  return(to_enum(:drop_last_while)) unless block_given?

  result   = []
  dropping = true
  reverse_each do |value|
    result.unshift(value) unless dropping &&= yield(value)
  end
  result
end
exactly?(n) { |*o| ... } click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 35
def exactly?(n)
  found_count = 0

  if block_given?
    each { |*o| found_count += 1 if yield(*o) }
  else
    each { |o| found_count += 1 if o }
  end

  (found_count > n) ? false : n == found_count
end
exponential(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 47
def exponential(identity=0)
  inject(:**) || identity
end
frequencies() click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 51
def frequencies
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end
max(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 55
def max(identity=0)
  size > 0 ? sort.last : identity
end
mean(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 63
def mean(identity=0)
  collection_size = size
  collection_size > 0 ? inject(:+) / collection_size.to_f : identity
end
median(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 68
def median(identity=0)
  collection_size   = size
  collection_sorted = sort

  if collection_size > 0
    if (collection_size % 2).zero?
      (collection_sorted[(collection_size / 2.0) -1.0] + collection_sorted[collection_size / 2.0]) / 2.0
    else
      collection_sorted[collection_size / 2.0]
    end
  else
    identity
  end
end
min(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 59
def min(identity=0)
  size > 0 ? sort.first : identity
end
mode(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 83
def mode(identity=0)
  if size > 0
    frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h }
    frequency_top_two      = frequency_distribution.sort { |a, b| b[1] <=> a[1] }.take(2)

    if frequency_top_two.length == 1
      frequency_top_two.first.first
    elsif frequency_top_two.first.last == frequency_top_two.last.last
      nil
    else
      frequency_top_two.first.first
    end
  else
    identity
  end
end
multiple(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 100
def multiple(identity=0)
  inject(:*) || identity
end
range(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 104
def range(identity=0)
  collection_sorted = sort
  size > 0 ? collection_sorted.last - collection_sorted.first : identity
end
several?() { |*o| ... } click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 109
def several?
  found_count = 0

  if block_given?
    each { |*o| found_count += 1 if yield(*o) }
  else
    each { |o| found_count += 1 if o }
  end

  (found_count > 1) ? true : false
end
standard_deviation() click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 121
def standard_deviation
  return(0) if length < 2
  Math.sqrt(variance)
end
sum(identity=0) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 4
def sum(identity=0)
  inject(:+) || identity
end
take_last(n) click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 126
def take_last(n)
  array = to_a

  return(array) if n > array.size
  array[(array.size - n)..-1]
end
take_last_while() { |e| ... } click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 133
def take_last_while
  return(to_enum(:take_last_while)) unless block_given?

  result = []
  reverse_each { |e| yield(e) ? result.unshift(e) : break }
  result
end
variance() click to toggle source
# File lib/nobiru/extensions/enumerable_extension.rb, line 141
def variance
  collection_length = length

  return(0) if collection_length <= 1
  sum = inject(0.0) { |accumulator, value| accumulator + (value - mean) ** 2.0 }
  sum / (collection_length.to_f - 1.0)
end