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

Leaf =

:nodoc:

Map.new

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Foldable

#length, #product, #sum

Class Method Details

+ (Object) [](h = {})

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

+ (Object) empty

Returns an empty map.



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

def self.empty
  Leaf
end

+ (Object) singleton(key, value)

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

- (Object) [](key)

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

- (Object) delete(key)

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

- (Object) each(&block)

Calls block once for each key/value in self.



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

def each(&block)
  foldl_with_key(nil) { |x, k, v| yield([k, v]) }
end

- (Object) foldl(e)

Folds the values in self from left to right.



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

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

- (Object) foldr(e)

Folds the values in self from right to left.



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

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

- (Object) insert(key, value)

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

- (String) inspect Also known as: to_s

Returns:

  • (String)


70
71
72
73
74
75
76
77
78
79
# File 'lib/immutable/map.rb', line 70

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

- (Object) map

Maps the given block over all values in self.



99
100
101
# File 'lib/immutable/map.rb', line 99

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

- (Object) map_with_key

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



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

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