class Hash

Public Instance Methods

map_hash() click to toggle source

map a hash into a hash

# File lib/functions/prelude_enumerable/hash.rb, line 29
def map_hash 
  Hash[ self.map{|k, v| yield(k,v) } ]
end
map_keys() click to toggle source

TODO discuss if we should not map onto array as values can colide

# File lib/functions/prelude_enumerable/hash.rb, line 20
def map_keys
  Hash[self.map{|k, v| [yield(k), v] }]
end
map_keys_and_values(km, vm) click to toggle source

map a hash into a hash recursively with a seperate key mapping and value mapping function

# File lib/functions/prelude_enumerable/hash.rb, line 40
def map_keys_and_values(km, vm)
  Hash[ self.map{|k, v| [ km.(k), v.is_a?(Hash) ? v.map_keys_and_values(km, vm) : vm.(v) ] } ]
end
map_keys_recurse(&b) click to toggle source
# File lib/functions/prelude_enumerable/hash.rb, line 24
def map_keys_recurse(&b)
  Hash[self.map{|k, v| [b.(k), v.is_a?(Hash) ? v.map_keys_recurse(&b) : v ] }]
end
map_recurse(&b) click to toggle source

map a hash into a hash recursively. the mapping function needs to check itself wether or not the value is hash and act appropriately

# File lib/functions/prelude_enumerable/hash.rb, line 35
def map_recurse(&b)
  Hash[ self.map{|k, v| b.(k, v.is_a?(Hash) ? v.map_recurse(&b) : v) } ]
end
map_values() click to toggle source
# File lib/functions/prelude_enumerable/hash.rb, line 11
def map_values 
  Hash[self.map{|k, v| [k, yield(v)] }]
end
map_values_recurse(&b) click to toggle source
# File lib/functions/prelude_enumerable/hash.rb, line 15
def map_values_recurse(&b)
  Hash[self.map{|k, v| [k, v.is_a?(Hash) ? v.map_values_recurse(&b) : b.(v)] }]
end
multi_map(&b) click to toggle source

map a hash into a hash recursively where one key value mapping can be mapped onto multi key value mappings

# File lib/functions/prelude_enumerable/hash.rb, line 45
def multi_map(&b)
  Hash[ self.map{|k, v| b.(k, ( v.is_a?(Hash) ? v.multi_map(&b) : v) ) }.flatten(1) ]
end
zip_hash_inner(bs) click to toggle source
# File lib/functions/prelude_enumerable/hash.rb, line 7
def zip_hash_inner(bs)
  self.each_with_object({}) { |(k, a), h| b = bs[k]; h[k] = [a, b] if b; h }
end
zip_hash_left(bs) click to toggle source
# File lib/functions/prelude_enumerable/hash.rb, line 3
def zip_hash_left(bs)
  self.each_with_object({}) { |(k, a), h| h[k] = [a, bs[k]]; h }
end