Class: Immutable::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/immutable/map.rb

Overview

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

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

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

Immutable::Map#insert inserts a key/value pair and returns a new Immutable::Map. The original map never be changed by Immutable::Map#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

Class Method Details

.[](h = {}) ⇒ Object

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



42
43
44
# File 'lib/immutable/map.rb', line 42

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

.emptyObject

Returns an empty map.



30
31
32
# File 'lib/immutable/map.rb', line 30

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.



36
37
38
# File 'lib/immutable/map.rb', line 36

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

Instance Method Details

#[](key) ⇒ Object

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

Raises:

  • (ScriptError)


53
54
55
# File 'lib/immutable/map.rb', line 53

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

#delete(key) ⇒ Object

Deletes key and its value from self.



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

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

#foldl(e) ⇒ Object

Folds the values in self from left to right.



84
85
86
# File 'lib/immutable/map.rb', line 84

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.



79
80
81
# File 'lib/immutable/map.rb', line 79

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

#insert(key, value) ⇒ Object

Inserts key/value in self.



47
48
49
# File 'lib/immutable/map.rb', line 47

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

#inspectObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/immutable/map.rb', line 67

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.



89
90
91
# File 'lib/immutable/map.rb', line 89

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

#map_with_keyObject

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



94
95
96
# File 'lib/immutable/map.rb', line 94

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