Map
The Map module provides functions for working with immutable key-value collections. Maps allow efficient lookup, insertion, and deletion by key.
Maps vs Records
Maps are dynamic collections where keys can be computed at runtime. For fixed-structure data with known field names, use records instead (see the Language Tour).
Creating Maps
Map.empty
Map k v
Returns an empty map.
m = Map.empty
println (Map.is-empty? m)
# => true
Map.from-list
List (k, v) -> Map k v
Creates a map from a list of key-value tuples.
m = Map.from-list [("a", 1), ("b", 2), ("c", 3)]
println (Map.get m "b")
# => Some(2)
Map.from-record
{...} -> Map String v
Creates a map from a record. Record field names become string keys.
m = Map.from-record {name: "Alice", age: 30}
println (Map.keys m)
# => ["name", "age"]
Accessing Values
Map.get
Map k v -> k -> Option v
Returns
Some value associated with the key, or None if the key is not found.
m = Map.from-list [("x", 10), ("y", 20)]
println (Map.get m "x")
# => Some(10)
println (Map.get m "z")
# => None
Map.keys
Map k v -> List k
Returns a list of all keys in the map.
m = Map.from-list [("a", 1), ("b", 2)]
println (Map.keys m)
# => ["a", "b"]
Map.values
Map k v -> List v
Returns a list of all values in the map.
m = Map.from-list [("a", 1), ("b", 2)]
println (Map.values m)
# => [1, 2]
Map.entries
Map k v -> List (k, v)
Returns a list of key-value tuples.
m = Map.from-list [("a", 1), ("b", 2)]
println (Map.entries m)
# => [("a", 1), ("b", 2)]
Modifying Maps
Map.insert
Map k v -> k -> v -> Map k v
Returns a new map with the key-value pair added or updated.
m1 = Map.from-list [("a", 1)]
m2 = Map.insert m1 "b" 2
println (Map.get m2 "b")
# => Some(2)
Map.delete
Map k v -> k -> Map k v
Returns a new map with the key removed.
m1 = Map.from-list [("a", 1), ("b", 2)]
m2 = Map.delete m1 "a"
println (Map.keys m2)
# => ["b"]
Map.merge
Map k v -> Map k v -> Map k v
Merges two maps. Values from the second map override values from the first for duplicate keys.
m1 = Map.from-list [("a", 1), ("b", 2)]
m2 = Map.from-list [("b", 3), ("c", 4)]
m3 = Map.merge m1 m2
println (Map.get m3 "b")
# => Some(3)
Querying
Map.contains? / Map.has-key?
Map k v -> k -> Bool
Returns
true if the map contains the key.
m = Map.from-list [("a", 1)]
println (Map.contains? m "a") # => true
println (Map.contains? m "b") # => false
Map.is-empty?
Map k v -> Bool
Returns
true if the map has no entries.
println (Map.is-empty? Map.empty) # => true
Map.is-map?
a -> Bool
Returns
true if the value is a map.
m = Map.from-list [("a", 1)]
println (Map.is-map? m) # => true
println (Map.is-map? [1, 2]) # => false
Map.size
Map k v -> Int
Returns the number of entries in the map.
m = Map.from-list [("a", 1), ("b", 2)]
println (Map.size m)
# => 2
Conversion
Map.to-list
Map k v -> List (k, v)
Converts a map to a list of key-value tuples (same as
entries).
m = Map.from-list [("a", 1)]
println (Map.to-list m)
# => [("a", 1)]
Map.to-str
Map k v -> String
Returns a string representation of the map.
m = Map.from-list [("a", 1)]
println (Map.to-str m)
# => "{a: 1}"