class Immutable::Map(K, V)

Included Modules

Direct Known Subclasses

Defined in:

immutable/map/trie.cr
immutable/map.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(hash : Hash(K, V) = {} of K => V) #

Creates a map with the given key-values

m = Immutable::Map.new({:a => 1, :b => true}) # Map {:a => 1, :b => true}

[View source]
def self.new(hash : Hash(K, V) = {} of K => V, &block : K -> V) #

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}

[View source]
def self.new(e : Enumerable(::Tuple(_, _))) #

Creates a map with the given key-values.

m = Immutable::Map.new([{:a, 123}, {:b, 321}]) # Map {:a => 123, :b => 321}

[View source]

Class Method Detail

def self.[](hash : Hash(K, V) = {} of K => V) #

Creates a map with the given key-values

m = Immutable::Map[{:a => 123, :b => 321}] # Map {:a => 123, :b => 321}

[View source]

Instance Method Detail

def ==(other : Map) #

[View source]
def [](key : K) #

Returns the value associated with the given key, if existing, else raises KeyError. See also #fetch


[View source]
def []?(key : K) #

Returns the value associated with the given key, if existing, else nil


[View source]
def delete(key : K) #

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}

[View source]
def each #

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"

[View source]
def each(&block) #

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

[View source]
def each_key #

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"

[View source]
def each_key(&block : K -> ) #

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

[View source]
def each_value #

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"

[View source]
def each_value(&block : V -> ) #

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

[View source]
def fetch(key : K, &block : K -> _) #

Returns the value associated with the given key, if existing, else executes the given block and returns its value


[View source]
def fetch(key : K, default) #

Returns the value associated with the given key, if existing, else it returns the provided default value


[View source]
def fetch(key : K) #

Returns the value associated with the given key, if existing, else raises KeyError


[View source]
def hash #

See Object#hash.

map = Immutable::Map[{"foo" => "bar"}]
map.hash # => 63502

[View source]
def inspect(io : IO) #

Appends a String representation of this object to the given IO object.


[View source]
def keys #

Returns only the keys as an Array. The order is not specified.

m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.keys # => ["foo", "bar"]

[View source]
def merge(map : Immutable::Map(L, W)) forall L, W #

[View source]
def merge(hash : Hash(L, W)) forall L, W #

[View source]
def merge(map : Immutable::Map(K, V)) #

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"}

[View source]
def merge(hash : Hash(K, V)) #

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"}

[View source]
def set(key : K, value : V) #

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}

[View source]
def size #

Returns the number of key-value pairs in the map


[View source]
def to_json(json : JSON::Builder) #

Returns the JSON serialization of this map


[View source]
def to_s(io : IO) #

Appends a String representation of this map to the given IO object.


[View source]
def 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.

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.


[View source]
def values #

Returns only the values as an Array. The order is not specified.

m = Immutable::Map[{"foo" => "bar", "baz" => "qux"}]
m.values # => ["bar", "qux"]

[View source]