Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/functions/prelude_enumerable/hash.rb
Instance Method Summary collapse
-
#map_hash ⇒ Object
map a hash into a hash.
-
#map_keys ⇒ Object
TODO discuss if we should not map onto array as values can colide.
-
#map_keys_and_values(km, vm) ⇒ Object
map a hash into a hash recursively with a seperate key mapping and value mapping function.
- #map_keys_recurse(&b) ⇒ Object
-
#map_recursive(&b) ⇒ Object
map a hash into a hash recursively.
- #map_values ⇒ Object
- #map_values_recurse(&b) ⇒ Object
-
#multi_map(&b) ⇒ Object
map a hash into a hash recursively where one key value mapping can be mapped onto multi key value mappings.
- #zip_hash_inner(bs) ⇒ Object
- #zip_hash_left(bs) ⇒ Object
Instance Method Details
#map_hash ⇒ Object
map a hash into a hash
51 52 53 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 51 def map_hash Hash[ self.map{|k, v| yield(k,v) } ] end |
#map_keys ⇒ Object
TODO discuss if we should not map onto array as values can colide
27 28 29 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 27 def map_keys Hash[self.map{|k, v| [yield(k), v] }] end |
#map_keys_and_values(km, vm) ⇒ Object
map a hash into a hash recursively with a seperate key mapping and value mapping function
62 63 64 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 62 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) ⇒ Object
31 32 33 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 31 def map_keys_recurse(&b) Hash[self.map{|k, v| [b.(k), v.is_a?(Hash) ? v.map_keys_recurse(&b) : v ] }] end |
#map_recursive(&b) ⇒ Object
map a hash into a hash recursively. the mapping function needs to check itself wether or not the value is hash and act appropriately
57 58 59 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 57 def map_recursive(&b) Hash[ self.map{|k, v| b.(k, v.is_a?(Hash) ? v.map_recursive(&b) : v) } ] end |
#map_values ⇒ Object
11 12 13 |
# 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) ⇒ Object
15 16 17 |
# 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) ⇒ Object
map a hash into a hash recursively where one key value mapping can be mapped onto multi key value mappings
67 68 69 |
# File 'lib/functions/prelude_enumerable/hash.rb', line 67 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) ⇒ Object
7 8 9 |
# 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) ⇒ Object
3 4 5 |
# 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 |