Map
Maps are Ard’s key-value collections. A map type is written [K:V], where K is the key type and V is the value type.
let scores: [Str:Int] = ["Ada": 10, "Grace": 12]let names: [Int:Str] = [1: "one", 2: "two"]Methods
Section titled “Methods”fn size() Int
Section titled “fn size() Int”Return the number of entries.
let scores = ["Ada": 10]let count = scores.size() // 1fn get(key: K) V?
Section titled “fn get(key: K) V?”Return the value for key, or Maybe::new<V>() if the key is absent.
let scores = ["Ada": 10]let ada = scores.get("Ada").or(0)let missing = scores.get("Grace") // Int?fn has(key: K) Bool
Section titled “fn has(key: K) Bool”Return whether key exists in the map.
let scores = ["Ada": 10]let ok = scores.has("Ada")fn keys() [K]
Section titled “fn keys() [K]”Return the map’s keys as a list. Go map iteration order is not deterministic, so do not rely on the key order.
let scores = ["Ada": 10, "Grace": 12]let names = scores.keys()fn set(key: K, value: V)
Section titled “fn set(key: K, value: V)”Set key to value through a map reference.
let scores = mut ["Ada": 10]scores.set("Grace", 12)fn delete(key: K)
Section titled “fn delete(key: K)”Remove key through a map reference. Deleting an absent key is allowed.
let scores = mut ["Ada": 10]scores.delete("Ada")Module helpers
Section titled “Module helpers”The ard/map module provides helper functions such as map::new. Import that module when you want those helpers; map methods are available without an import.