Class: Immutable::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Foldable
Defined in:
lib/immutable/map.rb

Overview

Immutable::Map represents an immutable map from keys to values.

Immutable::Map is an abstract class and Map.[] should be used instead of new. For example:

include Immutable
p Map[]            #=> Map[]
p Map[a: 1, b: 2]  #=> Map[:a => 1, :b => 2]

#insert inserts a key/value pair and returns a new Immutable::Map. The original map never be changed by #insert. For example:

m = Map[a: 1]
p m   #=> Map[:a => 1]
m2 = m.insert(:b, 2)
p m2  #=> Map[:a => 1, :b => 2]
p m   #=> Map[:a => 1]

Direct Known Subclasses

Fork

Defined Under Namespace

Classes: BlackFork, Fork, InvarianceViolationError, RedFork

Constant Summary collapse

Leaf =

:nodoc:

Map.new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Foldable

#length, #product, #sum

Class Method Details

.[](h = {}) ⇒ Object

Returns a map that has the same key/value pairs as the Hash object h.



44
45
46
# File 'lib/immutable/map.rb', line 44

def self.[](h = {})
  h.inject(Leaf) { |m, (k, v)| m.insert(k, v) }
end

.emptyObject

Returns an empty map.



32
33
34
# File 'lib/immutable/map.rb', line 32

def self.empty
  Leaf
end

.singleton(key, value) ⇒ Object

Returns a map that has only one pair whose key is key and whose value is value.



38
39
40
# File 'lib/immutable/map.rb', line 38

def self.singleton(key, value)
  Leaf.insert(key, value)
end

Instance Method Details

#==(x) ⇒ Boolean Also known as: ===

Returns:

  • (Boolean)


70
71
72
# File 'lib/immutable/map.rb', line 70

def ==(x)
  x.is_a?(self.class) && (to_h == x.to_h)
end

#[](key) ⇒ Object

Returns the value at key in self, or nil if key isn’t in self.

Raises:

  • (ScriptError)


55
56
57
# File 'lib/immutable/map.rb', line 55

def [](key)
  raise ScriptError, "this method should be overriden"
end

#delete(key) ⇒ Object

Deletes key and its value from self.



60
61
62
63
64
65
66
67
# File 'lib/immutable/map.rb', line 60

def delete(key)
  m = del(key)
  if m.empty?
    m
  else
    m.make_black
  end
end

#each {|key, element| ... } ⇒ self Also known as: each_pair

Calls block once for each key/value in self.

Yields:

  • (key, element)

Yield Returns:

  • (self)

Returns:

  • (self)


103
104
105
106
107
108
# File 'lib/immutable/map.rb', line 103

def each(&block)
  return to_enum(__callee__) unless block_given?

  foldl_with_key(nil) { |x, k, v| yield([k, v]) }
  self
end

#eql?(x) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/immutable/map.rb', line 76

def eql?(x)
  x.is_a?(self.class) && to_h.eql?(x.to_h)
end

#foldl(e) ⇒ Object

Folds the values in self from left to right.



118
119
120
# File 'lib/immutable/map.rb', line 118

def foldl(e)
  foldl_with_key(e) { |x, k, v| yield(x, v) }
end

#foldr(e) ⇒ Object

Folds the values in self from right to left.



113
114
115
# File 'lib/immutable/map.rb', line 113

def foldr(e)
  foldr_with_key(e) { |k, v, x| yield(v, x) }
end

#hashInteger

Returns:

  • (Integer)


81
82
83
# File 'lib/immutable/map.rb', line 81

def hash
  to_h.hash
end

#insert(key, value) ⇒ Object

Inserts key/value in self.



49
50
51
# File 'lib/immutable/map.rb', line 49

def insert(key, value)
  ins(key, value).make_black
end

#inspectString Also known as: to_s

Returns:

  • (String)


86
87
88
89
90
91
92
93
94
95
# File 'lib/immutable/map.rb', line 86

def inspect
  "Map[" + foldr_with_key("") { |k, v, s|
    x = k.inspect + " => " + v.inspect
    if s.empty?
      x
    else
      x + ", " + s
    end
  } + "]"
end

#mapObject

Maps the given block over all values in self.



123
124
125
# File 'lib/immutable/map.rb', line 123

def map
  map_with_key { |k, v| yield(v) }
end

#map_with_keyObject

Maps the given block over all keys and values in self.



128
129
130
# File 'lib/immutable/map.rb', line 128

def map_with_key
  foldr_with_key(List[]) { |k, v, xs| Cons[yield(k, v), xs] }
end

#to_hHash

Returns:

  • (Hash)


133
134
135
# File 'lib/immutable/map.rb', line 133

def to_h
  Hash[each_pair.to_a]
end