Class: Hash

Inherits:
Object show all
Extended by:
Monoid::ClassMethods
Includes:
Funkr::Categories, Monoid
Defined in:
lib/funkr/extensions/hash.rb

Overview

Extends Hash capabilities

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mzeroObject



35
# File 'lib/funkr/extensions/hash.rb', line 35

def mzero; self.new(); end

Instance Method Details

#map_kObject

maps function over hash keys



15
16
17
18
19
# File 'lib/funkr/extensions/hash.rb', line 15

def map_k # &block
  h = Hash.new(self.default)
  self.each{|k,v| h[yield(k)] = v}
  return h
end

#map_kvObject

maps function over hash keys and values. Block should take 2 parameters, and should return a 2 elements array.



23
24
25
26
27
28
29
30
# File 'lib/funkr/extensions/hash.rb', line 23

def map_kv # &block
  h = Hash.new(self.default)
  self.each do |k,v|
    nk, nv = yield(k,v)
    h[nk] = nv
  end
  return h
end

#map_vObject

maps function over hash values



8
9
10
11
12
# File 'lib/funkr/extensions/hash.rb', line 8

def map_v # &block
  h = Hash.new(self.default)
  self.each{|k,v| h[k] = yield(v)}
  return h
end

#mplus(m_y) ⇒ Object



49
50
51
# File 'lib/funkr/extensions/hash.rb', line 49

def mplus(m_y)
  self.merge(m_y){|k, v1, v2| v1.mplus(v2)}
end