Module: EverydayCliUtils::MapUtil
- Defined in:
- lib/everyday-cli-utils/safe/maputil.rb
Class Method Summary collapse
- .average(collection) ⇒ Object
- .chompall(collection) ⇒ Object
- .clone_hash(hash) ⇒ Object
- .expand(hash) ⇒ Object
- .extend_hash(base_hash, extending_hash) ⇒ Object
- .filtermap(collection, &block) ⇒ Object
- .floats(collection) ⇒ Object
- .hash_diff(hash1, hash2) ⇒ Object
- .hashmap(hash, &block) ⇒ Object
- .join(collection, join_str) ⇒ Object
- .prod(collection) ⇒ Object
- .productmap(collection, &block) ⇒ Object
- .removefalse(collection) ⇒ Object
- .std_dev(collection) ⇒ Object
- .sum(collection) ⇒ Object
- .summap(collection, &block) ⇒ Object
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 |
.clone_hash(hash) ⇒ Object
68 69 70 |
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 68 def self.clone_hash(hash) hashmap(hash) 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.(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 |
.extend_hash(base_hash, extending_hash) ⇒ Object
72 73 74 75 76 |
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 72 def self.extend_hash(base_hash, extending_hash) result_hash = clone_hash(extending_hash) base_hash.each { |v| result_hash[v[0]] = v[1] unless result_hash.has_key?(v[0]) } result_hash 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 |
.hash_diff(hash1, hash2) ⇒ Object
78 79 80 81 82 |
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 78 def self.hash_diff(hash1, hash2) new_hash = {} hash1.keys.each { |k| new_hash[k] = hash1[k] unless hash2.has_key?(k) && hash1[k] == hash2[k] } new_hash end |
.hashmap(hash, &block) ⇒ Object
62 63 64 65 66 |
# File 'lib/everyday-cli-utils/safe/maputil.rb', line 62 def self.hashmap(hash, &block) new_hash = {} block_given? ? hash.each { |v| new_hash[v[0]] = block.call(v) } : hash.each { |v| new_hash[v[0]] = v[1] } unless hash.nil? new_hash 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 |