module Enumerable

Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2017 License: GNU APGLv3 (or later) <www.gnu.org/copyleft/gpl.html>

Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>

Public Instance Methods

average() click to toggle source
# File lib/muflax/enumerable.rb, line 7
def average
  self.sum.to_f / self.size.to_f
end
Also aliased as: mean, avg
avg()
Alias for: average
count_by(&block) click to toggle source
# File lib/muflax/enumerable.rb, line 46
def count_by &block
  self.histogram(&block).sort_by(&:second).to_h
end
geometric_mean() click to toggle source
# File lib/muflax/enumerable.rb, line 13
def geometric_mean
  self.reduce(:*).to_f ** (1.0 / self.size.to_f)
end
histogram(&block) click to toggle source
# File lib/muflax/enumerable.rb, line 37
def histogram &block
  histo = vivaHash 0
  self.each do |el|
    histo[block.call(el)] += 1
  end

  histo
end
length_of_longest() click to toggle source
# File lib/muflax/align.rb, line 17
def length_of_longest
  self.longest.str_length
end
length_of_shortest() click to toggle source
# File lib/muflax/align.rb, line 21
def length_of_shortest
  self.shortest.str_length
end
longest() click to toggle source

find longest element

# File lib/muflax/align.rb, line 13
def longest
  self.max_by{|s| s.str_length}
end
mean()
Alias for: average
median() click to toggle source
# File lib/muflax/enumerable.rb, line 17
def median
  self.percentile 0.5
end
median_by(&block) click to toggle source
# File lib/muflax/enumerable.rb, line 21
def median_by &block
  self.percentile_by 0.5, &block
end
percentile(percent) click to toggle source
# File lib/muflax/enumerable.rb, line 25
def percentile percent
  self.sort[(self.size * percent).round]
end
percentile_by(percent, &block) click to toggle source
# File lib/muflax/enumerable.rb, line 29
def percentile_by percent, &block
  self.sort_by(&block)[(self.size * percent).round]
end
rfind(*arg, &block) click to toggle source
# File lib/muflax/enumerable.rb, line 50
def rfind *arg, &block
  self.reverse.find(*arg, &block)
end
shortest() click to toggle source

find shortest element

# File lib/muflax/align.rb, line 8
def shortest
  self.min_by{|s| s.str_length}
end
to_percent() click to toggle source
# File lib/muflax/enumerable.rb, line 59
def to_percent
  sum = self.sum / 100.0
  self.map{|n| (n.to_f / sum).round(1)}
end
to_ratio() click to toggle source
# File lib/muflax/enumerable.rb, line 54
def to_ratio
  sum = self.sum
  self.map{|n| (n.to_f / sum).round(1)}
end
triangle() click to toggle source
# File lib/muflax/enumerable.rb, line 33
def triangle
  self.to_a.triangle
end
with_previous() { |prev, cur;| ... } click to toggle source
# File lib/muflax/enumerable.rb, line 64
def with_previous
  self.reduce(nil){|prev, cur| yield prev, cur; cur}; self
end