Module: Enumerable
- Includes:
- CoreEx::Enumerable
- Defined in:
- lib/core_ex.rb,
lib/core_ex/enumerable.rb
Overview
class Pathname
Instance Method Summary collapse
- #average(init = 0) ⇒ Object
-
#each_pair(&block) ⇒ Object
Basically, do the same as each_with_index but yield the arguments in reverse order.
-
#fold(init, each_method = :each, &block) ⇒ Object
See also Enumerable#inject.
- #foldi(init, &block) ⇒ Object
- #group_sequences_by(&block) ⇒ Object
- #prod(init = 1) ⇒ Object
- #rec_fold(init, &block) ⇒ Object
-
#standard_deviation(init = 0, average_ = average(init)) ⇒ Object
fr: ecart type.
Instance Method Details
#average(init = 0) ⇒ Object
36 37 38 39 |
# File 'lib/core_ex/enumerable.rb', line 36 def average ( init=0 ) size_ = size.to_f inject(init) { |accu, x| accu + x / size_ } end |
#each_pair(&block) ⇒ Object
Basically, do the same as each_with_index but yield the arguments in reverse order. Thus an array can be assume in certain cases to a hash.
52 53 54 |
# File 'lib/core_ex/enumerable.rb', line 52 def each_pair ( &block ) each_with_index { |x, i| block[i, x] } end |
#fold(init, each_method = :each, &block) ⇒ Object
See also Enumerable#inject
17 18 19 20 21 22 23 |
# File 'lib/core_ex/enumerable.rb', line 17 def fold ( init, each_method=:each, &block ) accu = init send(each_method) do |*a| accu = block[accu, *a] end accu end |
#foldi(init, &block) ⇒ Object
26 27 28 |
# File 'lib/core_ex/enumerable.rb', line 26 def foldi ( init, &block ) fold(init, :each_with_index, &block) end |
#group_sequences_by(&block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/core_ex/enumerable.rb', line 63 def group_sequences_by ( &block ) result = [] current = nil each do |x| if current.nil? result << [x] else if block[current, x] result.last << x else result << [x] end end current = x end result end |
#prod(init = 1) ⇒ Object
31 32 33 |
# File 'lib/core_ex/enumerable.rb', line 31 def prod ( init=1 ) inject(init) { |accu, x| accu * x } end |
#rec_fold(init, &block) ⇒ Object
57 58 59 60 61 |
# File 'lib/core_ex/enumerable.rb', line 57 def rec_fold ( init, &block ) fold(init) do |accu1, *a1| a1.fold(accu1) { |accu2, a2| a2.rec_fold(accu2, &block) } end end |
#standard_deviation(init = 0, average_ = average(init)) ⇒ Object
fr: ecart type
43 44 45 46 47 |
# File 'lib/core_ex/enumerable.rb', line 43 def standard_deviation ( init=0, average_=average(init) ) size_ = size.to_f sigma = inject(init) { |accu, x| accu + ((x - average_) ** 2) / size_ } Math.sqrt sigma end |