Skip to content

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

Return the number of entries.

let scores = ["Ada": 10]
let count = scores.size() // 1

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?

Return whether key exists in the map.

let scores = ["Ada": 10]
let ok = scores.has("Ada")

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()

Set key to value in a mutable map.

mut scores: [Str:Int] = [:]
scores.set("Ada", 10)

Remove key from a mutable map. Deleting an absent key is allowed.

mut scores = ["Ada": 10]
scores.delete("Ada")

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.