class Immutable::Map(K, V)
- Immutable::Map(K, V)
- Reference
- Object
Included Modules
- Enumerable({K, V})
Direct Known Subclasses
Defined in:
immutable/map/trie.crimmutable/map.cr
Constructors
-
.new(hash : Hash(K, V) = {} of K => V)
Creates a map with the given key-values
-
.new(hash : Hash(K, V) = {} of K => V, &block : K -> V)
Creates a map with the given key-values.
-
.new(e : Enumerable(::Tuple(_, _)))
Creates a map with the given key-values.
Class Method Summary
-
.[](hash : Hash(K, V) = {} of K => V)
Creates a map with the given key-values
Instance Method Summary
- #==(other : Map)
-
#[](key : K)
Returns the value associated with the given key, if existing, else raises
KeyError
. -
#[]?(key : K)
Returns the value associated with the given key, if existing, else nil
-
#delete(key : K)
Returns a modified copy of the map with the key-value pair removed.
-
#each
Returns an iterator over the map entries, returning a
Tuple
of the key and value. -
#each(&block)
Calls the given block for each key-value and passes in a tuple of key and value.
-
#each_key
Returns an iterator over the map keys.
-
#each_key(&block : K -> )
Calls the given block for each key-value pair and passes in the key.
-
#each_value
Returns an iterator over the map values.
-
#each_value(&block : V -> )
Calls the given block for each key-value pair and passes in the value.
-
#fetch(key : K, &block : K -> _)
Returns the value associated with the given key, if existing, else executes the given block and returns its value
-
#fetch(key : K, default)
Returns the value associated with the given key, if existing, else it returns the provided default value
-
#fetch(key : K)
Returns the value associated with the given key, if existing, else raises
KeyError
-
#hash
See
Object#hash
. -
#inspect(io : IO)
Appends a
String
representation of this object to the given IO object. -
#keys
Returns only the keys as an
Array
. - #merge(map : Immutable::Map(L, W)) forall L, W
- #merge(hash : Hash(L, W)) forall L, W
-
#merge(map : Immutable::Map(K, V))
Returns a new map with the keys and values of this map and the given map combined.
-
#merge(hash : Hash(K, V))
Returns a new map with the keys and values of this map and the given hash combined.
-
#set(key : K, value : V)
Returns a modified copy of the map where key is associated to value
-
#size
Returns the number of key-value pairs in the map
-
#to_json(json : JSON::Builder)
Returns the JSON serialization of this map
-
#to_s(io : IO)
Appends a String representation of this map to the given IO object.
-
#transient(&block)
Executes the given block passing a transient version of the map, then converts the transient map back to an immutable one and returns it.
-
#values
Returns only the values as an
Array
.
Constructor Detail
Creates a map with the given key-values
m = Immutable::Map.new({:a => 1, :b => true}) # Map {:a => 1, :b => true}
Creates a map with the given key-values. When getting a key-value that is not in the map, the given block is executed passing the key, and the return value is returned.
m = Immutable::Map.new({:a => 123, :b => 321 }) # Map {:a => 123, :b => 321}
Creates a map with the given key-values.
m = Immutable::Map.new([{:a, 123}, {:b, 321}]) # Map {:a => 123, :b => 321}
Class Method Detail
Creates a map with the given key-values
m = Immutable::Map[{:a => 123, :b => 321}] # Map {:a => 123, :b => 321}
Instance Method Detail
Returns the value associated with the given key, if existing, else raises
KeyError
. See also #fetch
Returns a modified copy of the map with the key-value pair removed. If the
key is not existing, it raises KeyError
m = Immutable::Map[{:foo => 123, :bar => 321 }]
m2 = m.delete(:bar) # => Map {:foo => 123}
m # => Map {:foo => 123, bar: 321}
Returns an iterator over the map entries, returning a Tuple
of the key
and value. The order of iteration is not specified.
map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each
entry = iterator.next
entry[0] # => "foo"
entry[1] # => "bar"
entry = iterator.next
entry[0] # => "baz"
entry[1] # => "qux"
Calls the given block for each key-value and passes in a tuple of key and value. The order of iteration is not specified.
m = Immutable::Map[{"foo" => "bar"}]
m.each do |keyval|
keyval[0] # => "foo"
keyval[1] # => "bar"
end
Returns an iterator over the map keys. The order is not guaranteed.
map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each_key
key = iterator.next
key # => "foo"
key = iterator.next
key # => "baz"
Calls the given block for each key-value pair and passes in the key.
m = Immutable::Map[{"foo" => "bar"}]
m.each_key do |key|
key # => "foo"
end
Returns an iterator over the map values. The order is not specified.
map = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
iterator = map.each_value
val = iterator.next
val # => "bar"
val = iterator.next
val # => "qux"
Calls the given block for each key-value pair and passes in the value.
m = Immutable::Map[{"foo" => "bar"}]
m.each_value do |val|
val # => "bar"
end
Returns the value associated with the given key, if existing, else executes the given block and returns its value
Returns the value associated with the given key, if existing, else it returns the provided default value
Returns the value associated with the given key, if existing, else raises
KeyError
Returns only the keys as an Array
. The order is not specified.
m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.keys # => ["foo", "bar"]
Returns a new map with the keys and values of this map and the given map combined. A value in the given map takes precedence over the one in this map.
map = Immutable::Map[{"foo" => "bar"}]
merged = map.merge(Immutable::Map[{"baz" => "qux"}])
merged # => Map {"foo" => "bar", "baz" => "qux"}
map # => Map {"foo" => "bar"}
Returns a new map with the keys and values of this map and the given hash combined. A value in the given hash takes precedence over the one in this map.
map = Immutable::Map[{"foo" => "bar"}]
merged = map.merge({"baz" => "qux"})
merged # => Map {"foo" => "bar", "baz" => "qux"}
map # => Map {"foo" => "bar"}
Returns a modified copy of the map where key is associated to value
m = Immutable::Map[{:foo => 123}]
m2 = m.set(:bar, 321) # => Map {:foo => 123, :bar => 321}
m # => Map {:foo => 123}
Executes the given block passing a transient version of the map, then converts the transient map back to an immutable one and returns it.
This is useful to perform several updates on a map in an efficient way: as the transient map supports the same API of map, but performs updates in place, avoiding unnecessary object allocations.
map = Immutable::Map(Int32, Int32).new
m2 = map.transient do |m|
100.times { |i| m = m.set(i, i * 2) }
end
m2.size # => 100
Note that, as the transient is mutable, it is not thread-safe.
Returns only the values as an Array
. The order is not specified.
m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.values # => ["bar", "qux"]