Skip to content

Dicts ​

Keys keep insertion order, in every function and in JSON. Values are read with [], get, fetch or dig: a dot on a dict is always a function call, never a field.

mzs
d = {name: "Ann", age: 30}
d["name"]  # → "Ann"
d["zz"]  # → nil
d.keys  # → ["name","age"]
{"a b": 1, 2 -> "two"}  # → {"a b":1,"2":"two"}

A non-string key is written with ->; a computed key with (k): v. A bare word before : is a string, not a variable.

mzs
k = "x"
{k: 1}  # → {"k":1}
{(k): 1}  # → {"x":1}
{1 -> "A", 2 -> "B"}[1]  # → "A"

Reading ​

FunctionWhat it does
len / emptynumber of keys / whether empty
keys / valuesan array of keys / values
has(k)whether the key is there (same as k in d)
has_val(v)whether the value is there
get(k, default = nil)the value or the fallback; never fails
fetch(k)the value, or a key error
dig(*keys)a path through nested dicts and arrays; nil if missing
mzs
d = {a: 1, b: 2}
d.len  # → 2
d.values  # → [1,2]
d.has("a")  # → true
"b" in d  # → true
d.has_val(2)  # → true
d.get("z", 0)  # → 0
d.fetch("a")  # → 1
{a: {b: [10, 20]}}.dig("a", "b", 1)  # → 20
{a: 1}.dig("x", "y")  # → nil
mzs
{a: 1}.fetch("b")
text
key not found: "b"

Change the dict ✎ ​

FunctionWhat it does
d[k] = v / set(k, v)✎ writes the key; set returns the dict
delete(k)✎ removes the key, returns the value or nil
merge_in_place(*ds)✎ merges dicts in, later ones win
mzs
d = {a: 1}
d["b"] = 2
d.set("c", 3)  # → {"a":1,"b":2,"c":3}
d.delete("a")  # → 1
d.merge_in_place({b: 9})  # → {"b":9,"c":3}
d  # → {"b":9,"c":3}

Merging and picking ​

FunctionWhat it does
merge(*ds) / a + ba new dict; later keys win
pick(*keys)only these keys
omit(*keys)all but these
invertvalues become keys
dupa shallow copy
mzs
{a: 1}.merge({a: 9}, {b: 2})  # → {"a":9,"b":2}
{a: 1} + {b: 2}  # → {"a":1,"b":2}
d = {a: 1, b: 2, c: 3}
d.pick("c", "a")  # → {"a":1,"c":3}
d.omit("a")  # → {"b":2,"c":3}
{a: 1}.invert  # → {"1":"a"}

Iterating ​

The closure gets the key and the value. An unused parameter is _.

FunctionReturns
each { (k, v) -> … }the dict itself
map { (k, v) -> … }an array of results
map_values { … }a dict with the same keys; the closure gets the value
filter { … } / reject { … }a dict
find { … }a [k, v] pair, or nil
any / all / none / countyes/no, a number
sort_by { … }an array of pairs
arrayan array of [k, v] pairs
mzs
d = {a: 1, b: 2, c: 3}
d.map { (k, v) -> "${k}=${v}" }.join("&")  # → "a=1&b=2&c=3"
d.map_values { it * 10 }  # → {"a":10,"b":20,"c":30}
d.filter { (_, v) -> v > 1 }  # → {"b":2,"c":3}
d.reject { (_, v) -> v > 1 }  # → {"a":1}
d.find { (_, v) -> v > 1 }  # → ["b",2]
d.any { (k) -> k == "b" }  # → true
d.count { (_, v) -> v > 1 }  # → 2
d.sort_by { (_, v) -> -v }  # → [["c",3],["b",2],["a",1]]
d.array  # → [["a",1],["b",2],["c",3]]

for over a dict yields pairs; two variables unpack them.

mzs
for k, v in {a: 1, b: 2} { println("${k} → ${v}") }
text
a → 1
b → 2

Folding and grouping ​

FunctionWhat it does
reduce(init) { (acc, k, v) -> … }folds; init is required
sum { (k, v) -> … }the sum of the results
min_by { … } / max_by { … }the pair with the smallest / largest closure result
group_by { … }a dict of dicts
partition { … }[matching, the rest]
mzs
d = {a: 1, b: 2, c: 3}
d.reduce(0) { (acc, _, v) -> acc + v }  # → 6
d.sum { (_, v) -> v * 2 }  # → 12
d.max_by { (_, v) -> v }  # → ["c",3]
d.partition { (_, v) -> v > 1 }  # → [{"b":2,"c":3},{"a":1}]

Counting ​

A common move: tally on an array, or a counter through get:

mzs
words = "a b a c a".split
words.tally  # → {"a":3,"b":1,"c":1}
counts = {}
for w in words { counts[w] = counts.get(w, 0) + 1 }
counts  # → {"a":3,"b":1,"c":1}