Class: Immutable::Map::Fork
- Inherits:
-
Immutable::Map
- Object
- Immutable::Map
- Immutable::Map::Fork
- Defined in:
- lib/immutable/map.rb
Overview
:nodoc:
Constant Summary
Constants inherited from Immutable::Map
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #deconstruct ⇒ Object
- #del(key) ⇒ Object
- #empty? ⇒ Boolean
- #foldl_with_key(e, &block) ⇒ Object
- #foldr_with_key(e, &block) ⇒ Object
-
#initialize(left, key, value, right) ⇒ Fork
constructor
A new instance of Fork.
Methods inherited from Immutable::Map
#==, #delete, #each, empty, #eql?, #foldl, #foldr, #hash, #insert, #inspect, #map, #map_with_key, singleton, #to_h
Methods included from Foldable
#foldl, #length, #product, #sum
Constructor Details
#initialize(left, key, value, right) ⇒ Fork
Returns a new instance of Fork.
167 168 169 170 171 172 |
# File 'lib/immutable/map.rb', line 167 def initialize(left, key, value, right) @left = left @key = key @value = value @right = right end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
165 166 167 |
# File 'lib/immutable/map.rb', line 165 def key @key end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
165 166 167 |
# File 'lib/immutable/map.rb', line 165 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
165 166 167 |
# File 'lib/immutable/map.rb', line 165 def right @right end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
165 166 167 |
# File 'lib/immutable/map.rb', line 165 def value @value end |
Instance Method Details
#[](key) ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/immutable/map.rb', line 182 def [](key) x = key <=> @key if x < 0 @left[key] elsif x > 0 @right[key] else @value end end |
#deconstruct ⇒ Object
221 222 223 |
# File 'lib/immutable/map.rb', line 221 def deconstruct [@left, @key, @value, @right] end |
#del(key) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/immutable/map.rb', line 193 def del(key) if key < self.key del_left(left, self.key, self.value, right, key) elsif key > self.key del_right(left, self.key, self.value, right, key) else app(left, right) end end |
#empty? ⇒ Boolean
178 179 180 |
# File 'lib/immutable/map.rb', line 178 def empty? false end |
#foldl_with_key(e, &block) ⇒ Object
216 217 218 219 |
# File 'lib/immutable/map.rb', line 216 def foldl_with_key(e, &block) l = @left.foldl_with_key(e, &block) @right.foldl_with_key(yield(l, @key, @value), &block) end |
#foldr_with_key(e, &block) ⇒ Object
207 208 209 210 |
# File 'lib/immutable/map.rb', line 207 def foldr_with_key(e, &block) r = @right.foldr_with_key(e, &block) @left.foldr_with_key(yield(@key, @value, r), &block) end |