Module: EverydayCliUtils::MapUtil

Defined in:
lib/everyday-cli-utils/safe/maputil.rb

Class Method Summary collapse

Class Method Details

.average(collection) ⇒ Object



19
20
21
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 19

def self.average(collection)
  sum(collection).to_f / collection.count.to_f
end

.chompall(collection) ⇒ Object



42
43
44
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 42

def self.chompall(collection)
  collection.map(&:chomp)
end

.expand(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 50

def self.expand(hash)
  rval = {}
  hash.each { |v|
    if v[0].is_a? Array
      v[0].each { |v2| rval[v2] = v[1] }
    else
      rval[v[0]] = v[1]
    end
  }
  rval
end

.filtermap(collection, &block) ⇒ Object



7
8
9
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 7

def self.filtermap(collection, &block)
  removefalse(collection.map(&block))
end

.floats(collection) ⇒ Object



30
31
32
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 30

def self.floats(collection)
  collection.map(&:to_f)
end

.join(collection, join_str) ⇒ Object



46
47
48
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 46

def self.join(collection, join_str)
  collection.map(&:to_s).reduce { |a, b| a << join_str << b }
end

.prod(collection) ⇒ Object



15
16
17
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 15

def self.prod(collection)
  collection.reduce(:*)
end

.productmap(collection, &block) ⇒ Object



38
39
40
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 38

def self.productmap(collection, &block)
  prod(collection.map(&block))
end

.removefalse(collection) ⇒ Object



3
4
5
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 3

def self.removefalse(collection)
  collection.select { |i| i }
end

.std_dev(collection) ⇒ Object



23
24
25
26
27
28
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 23

def self.std_dev(collection)
  avg = average(collection)
  cnt = collection.count.to_f
  su  = summap(collection) { |v| (v.to_f - avg.to_f) ** 2 }
  Math.sqrt(su / cnt)
end

.sum(collection) ⇒ Object



11
12
13
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 11

def self.sum(collection)
  collection.reduce(:+)
end

.summap(collection, &block) ⇒ Object



34
35
36
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 34

def self.summap(collection, &block)
  sum(collection.map(&block))
end