Skip to content

Arrays ​

An array is a reference: assignment does not copy. Only the functions of the first section change an array; the rest return a new one.

mzs
xs = [1, 2]
ys = xs
ys.push(3)
xs  # → [1,2,3]
[1, 2] + [3]  # → [1,2,3]
[1, 2, 2, 3] - [2]  # → [1,3]
[1, 2, 3][-1]  # → 3
[1, 2, 3, 4][1..2]  # → [2,3]

Change the array ✎ ​

FunctionWhat it does
push(*v) / unshift(*v)✎ adds to the end / the start, returns the array
insert(i, *v)✎ inserts before position i
concat(*arrays)✎ appends arrays to the end (+ does not change)
pop / shift✎ removes the last / first, returns it or nil
delete_at(i)✎ removes by index, returns the element
delete(v)✎ removes all elements equal to v
sort_in_place / reverse_in_place✎ sorts / reverses in place
mzs
xs = [3, 1]
xs.push(2)  # → [3,1,2]
xs.pop  # → 2
xs.unshift(0)  # → [0,3,1]
xs.shift  # → 0
xs.insert(1, 9)  # → [3,9,1]
xs.delete_at(1)  # → 9
xs.concat([5, 3])  # → [3,1,5,3]
xs.delete(3)  # → [1,5]
xs.sort_in_place  # → [1,5]
xs  # → [1,5]

Access ​

FunctionWhat it does
len / emptylength / whether empty
first(n) / last(n)the first / last element, or n of them
has(v)whether the element is there (same as v in xs)
index(v) / index { … }position of the element, or nil
count(v) / count { … }how many are equal or match
dig(*keys)a path through nested arrays and dicts; nil if missing
mzs
[1, 2, 3].first  # → 1
[1, 2, 3].last(2)  # → [2,3]
[].first  # → nil
[1, 2, 3].has(2)  # → true
2 in [1, 2, 3]  # → true
[1, 2, 3].index(3)  # → 2
[1, 2, 3].index { it > 1 }  # → 1
[1, 2, 2, 3].count(2)  # → 2
[1, 2, 3, 4].count { it.even }  # → 2
[[1, 2], [3]].dig(0, 1)  # → 2
[{a: 1}].dig(5, "a")  # → nil

Transforming ​

FunctionWhat it does
map { … }a new array of the results
flat_map { … }map that unpacks one level
filter { … } / reject { … }keeps / drops the matching ones
compactwithout nil
flatten(depth)unpacks nested arrays
reversereversed
join(sep = "")a string of the elements' str
each { … } / each_with_index { (x, i) -> … }iterating; returns the array
mzs
[1, 2, 3].map { it * 2 }  # → [2,4,6]
[1, 2].flat_map { [it, it] }  # → [1,1,2,2]
[1, 2, 3, 4].filter { it.even }  # → [2,4]
[1, 2, 3, 4].reject { it.even }  # → [1,3]
[1, nil, 2].compact  # → [1,2]
[1, [2, [3]]].flatten  # → [1,2,3]
[1, [2, [3]]].flatten(1)  # → [1,2,[3]]
[1, 2, 3].reverse  # → [3,2,1]
["a", "b"].join("-")  # → "a-b"
mzs
["a", "b"].each_with_index { (x, i) -> println("${i}: ${x}") }
text
0: a
1: b

Search and checks ​

FunctionWhat it does
find { … }the first match, or nil
any { … } / all { … } / none { … }any / all / none match
mzs
[1, 2, 3].find { it > 1 }  # → 2
[1, 2, 3].find { it > 9 }  # → nil
[1, 2, 3].any { it > 2 }  # → true
[1, 2].all { it > 0 }  # → true
[1, 2].none { it > 5 }  # → true
[0, nil].any  # → true

Folding and grouping ​

FunctionWhat it does
reduce(init) { (acc, x) -> … }folds into one value
sum / sum { … }the sum
mean / median / productmean (always a float) / median / product
tallya dict "element → how many times"
group_by { … }a dict "key → elements"
partition { … }[matching, the rest]
accumulate(init) { … }reduce that keeps every step
mzs
[1, 2, 3].reduce { (a, x) -> a + x }  # → 6
[1, 2, 3].reduce(10) { (a, x) -> a + x }  # → 16
[{n: 1}, {n: 2}].sum { it["n"] }  # → 3
[1, 2, 3, 4].mean  # → 2.5
[4, 1, 3, 2].median  # → 2.5
[2, 3, 4].product  # → 24
["a", "b", "a"].tally  # → {"a":2,"b":1}
[1, 2, 3, 4].group_by { it.even ? "even" : "odd" }  # → {"odd":[1,3],"even":[2,4]}
[1, 2, 3, 4].partition { it.even }  # → [[2,4],[1,3]]
[1, 2, 3, 4].accumulate { (a, b) -> a + b }  # → [1,3,6,10]

Sorting, minimum, maximum ​

sort, min, max take a comparator { (a, b) -> a <=> b }; sort_by, min_by, max_by take a key { it… }. The sort is stable.

mzs
[3, 1, 2].sort  # → [1,2,3]
[3, 1, 2].sort { (a, b) -> b <=> a }  # → [3,2,1]
["bb", "a", "ccc"].sort_by { it.len }  # → ["a","bb","ccc"]
[{n: 2}, {n: 1}].sort_by { -it["n"] }  # → [{"n":2},{"n":1}]
[3, 1, 2].min  # → 1
["aa", "b"].min_by { it.len }  # → "b"
["a", "bbb"].max_by { it.len }  # → "bbb"
[].max  # → nil

Sets ​

The result has no repeats, in order of first occurrence.

FunctionWhat it does
uniq / uniq { … }without repeats (by value or by key)
union(*xs) / intersect(*xs) / difference(*xs)union / intersection / difference
subset(other)whether all elements are in other
to_seta dict {element: true} for a fast has
mzs
[1, 1, 2, 1].uniq  # → [1,2]
[[1, "a"], [2, "a"]].uniq { it[1] }  # → [[1,"a"]]
[1, 1, 2].union([2, 3])  # → [1,2,3]
[1, 2, 3].intersect([2, 3, 4])  # → [2,3]
[1, 1, 2, 3].difference([3])  # → [1,2]
[1, 2].subset([3, 2, 1])  # → true
["a", "b"].to_set  # → {"a":true,"b":true}

Slices and windows ​

FunctionWhat it does
slice(i, n = 1)n elements from position i
take(n) / drop(n)the first n / everything after them
take_while { … } / drop_while { … }while the condition holds
each_slice(n)chunks of n
each_cons(n)sliding windows of n
chunk_while { (a, b) -> … }cuts where the condition is false
rotate(n = 1)cyclic shift
mzs
[1, 2, 3].slice(1, 2)  # → [2,3]
[1, 2, 3, 4].take(2)  # → [1,2]
[1, 2, 3, 4].drop(2)  # → [3,4]
[1, 2, 3, 1].take_while { it < 3 }  # → [1,2]
[1, 2, 3, 4, 5].each_slice(2)  # → [[1,2],[3,4],[5]]
[1, 2, 3].each_cons(2)  # → [[1,2],[2,3]]
[1, 2, 4, 5, 7].chunk_while { (a, b) -> b == a + 1 }  # → [[1,2],[4,5],[7]]
[1, 2, 3, 4].rotate  # → [2,3,4,1]

Zipping and pairs ​

FunctionWhat it does
zip(*xs)joins element by element; short ones are padded with nil
transposerows ↔ columns
dicta dict from [k, v] pairs
pack_bytesa string from bytes (the reverse of bytes)
mzs
[1, 2].zip([3, 4])  # → [[1,3],[2,4]]
[1, 2, 3].zip(["a"])  # → [[1,"a"],[2,null],[3,null]]
[[1, 2], [3, 4]].transpose  # → [[1,3],[2,4]]
["a", "b"].zip([1, 2]).dict  # → {"a":1,"b":2}
[72, 105].pack_bytes  # → "Hi"

Random ​

shuffle and sample exist only if the host allows randomness (--rand).

mzs
[1, 2, 3].shuffle.sort  # → [1,2,3]
[1, 2, 3].sample in [1, 2, 3]  # → true