Module: Enumerable

Defined in:
lib/mharris_ext/enumerable.rb,
lib/mharris_ext/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#avg_b(&b) ⇒ Object



18
19
20
# File 'lib/mharris_ext/enumerable.rb', line 18

def avg_b(&b)
  sum_b(&b).to_f / size.to_f
end

#nths(num) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mharris_ext/enumerable.rb', line 46

def nths(num)
  res = []
  s = e_f = e_i = 0
  n_size = size.to_f / num.to_f
  total_segment_size = 0
  (0...num).each do |n|
    s = e_i
    e_f = e_f + n_size
    e_i = (e_f+0.5).to_i
    seg = self[s...e_i]
   # puts %w(s e_f e_i seg).map { |x| "#{x}: #{send(x)}" }.join("\n")
    #puts "s: #{s}, e_f: #{e_f}, e_i: #{e_i}, seg: #{seg.inspect}"
    res << seg
    total_segment_size += res[n].size
  end
  unless total_segment_size == size
    puts inspect
    puts res.inspect
    raise "nths_hash is bad" 
  end
  res
end

#nths_hash(num) ⇒ Object



68
69
70
71
72
# File 'lib/mharris_ext/enumerable.rb', line 68

def nths_hash(num)
  res = {}
  nths(num).each_with_index { |x,i| res[i] = x }
  res
end

#nths_hashx(num) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mharris_ext/enumerable.rb', line 24

def nths_hashx(num)
  res = {}
  s = e_f = e_i = 0
  n_size = size.to_f / num.to_f
  total_segment_size = 0
  (0...num).each do |n|
    s = e_i
    e_f = e_f + n_size
    e_i = (e_f+0.5).to_i
    seg = self[s...e_i]
   # puts %w(s e_f e_i seg).map { |x| "#{x}: #{send(x)}" }.join("\n")
    #puts "s: #{s}, e_f: #{e_f}, e_i: #{e_i}, seg: #{seg.inspect}"
    res[n] = seg
    total_segment_size += res[n].size
  end
  unless total_segment_size == size
    puts inspect
    puts res.inspect
    raise "nths_hash is bad" 
  end
  res
end

#sum_bObject



15
16
17
# File 'lib/mharris_ext/enumerable.rb', line 15

def sum_b
  map { |x| yield(x) }.sum
end

#sumxObject



10
11
12
13
14
# File 'lib/mharris_ext/enumerable.rb', line 10

def sumx
  res = 0
  each { |x| res += x }
  res
end