Make a map #

A map links keys to values. Keys are Int, String or Bytes. All keys have one type, and all values have one type: Map[K, V]. A map keeps the order in which keys were added.

def main() -> Void
  stock = {"apple": 3, "pear": 0}
  puts(stock)
  puts(stock.size)
end
Output
{"apple": 3, "pear": 0}
2

An empty map needs a known type:

def main() -> Void
  ages: Map[String, Int] = {}
  ages = ages.put("Ana", 25)
  puts(ages)
end
Output
{"Ana": 25}

Read a key #

map[key] gives the value. A missing key stops the program.

def main() -> Void
  stock = {"apple": 3}
  puts(stock["apple"])
end
Output
3

get with a default never fails. get without one gives an Optional: some(value) or none.

def main() -> Void
  stock = {"apple": 3}
  puts(stock.get("kiwi", 0))
  puts(stock.get("apple"))
  puts(stock.get("kiwi").none?)
  puts(stock.key?("apple"))
end
Output
0
some(3)
true
true

Add, change and remove #

put gives a new map with the entry added or replaced. delete gives a new map without the key. The old map does not change.

def main() -> Void
  stock = {"apple": 3}
  more = stock.put("pear", 5)
  more = more.put("apple", 4)
  less = more.delete("pear")
  puts(stock)
  puts(more)
  puts(less)
end
Output
{"apple": 3}
{"apple": 4, "pear": 5}
{"apple": 4}

Walk a map #

keys and values give lists in insertion order.

def main() -> Void
  prices = {"tea": 3, "cake": 5}
  for name in prices.keys
    puts("#{name}: #{prices[name]}")
  end

  puts(prices.values)
end
Output
tea: 3
cake: 5
[3, 5]

Count things #

def main() -> Void
  counts: Map[String, Int] = {}
  for word in "a b a c a".split(" ")
    counts = counts.put(word, counts.get(word, 0) + 1)
  end

  puts(counts)
end
Output
{"a": 3, "b": 1, "c": 1}

For fixed fields of different types, use a struct. Every function is on the Map library page.