module EverydayCliUtils::MapUtil
Public Class Methods
average(collection)
click to toggle source
# 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)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 42 def self.chompall(collection) collection.map(&:chomp) end
clone_hash(hash)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 68 def self.clone_hash(hash) hashmap(hash) end
expand(hash)
click to toggle source
# 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
extend_hash(base_hash, extending_hash)
click to toggle source
# 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)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 7 def self.filtermap(collection, &block) removefalse(collection.map(&block)) end
floats(collection)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 30 def self.floats(collection) collection.map(&:to_f) end
hash_diff(hash1, hash2)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 15 def self.prod(collection) collection.reduce(:*) end
productmap(collection, &block)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 38 def self.productmap(collection, &block) prod(collection.map(&block)) end
removefalse(collection)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 3 def self.removefalse(collection) collection.select { |i| i } end
std_dev(collection)
click to toggle source
# 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)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 11 def self.sum(collection) collection.reduce(:+) end
summap(collection, &block)
click to toggle source
# File lib/everyday-cli-utils/safe/maputil.rb, line 34 def self.summap(collection, &block) sum(collection.map(&block)) end