Types, part II

In Typst, most of things are immutable. You can't change content, you can just create new using this one (for example, using addition).

Immutability is very important for Typst since it tries to be as pure language as possible. Functions do nothing outside of returning some value.

However, purity is partly "broken" by these types. They are super-useful and not adding them would make Typst much pain.

However, using them adds complexity.

Arrays (array)

Link to Reference.

Mutable object that stores data with their indices.

Working with indices

#let values = (1, 7, 4, -3, 2)

// take value at index 0
#values.at(0) \
// set value at 0 to 3
#(values.at(0) = 3)
// negative index => start from the back
#values.at(-1) \
// add index of something that is even
#values.find(calc.even)
Rendered image

Iterating methods

#let values = (1, 7, 4, -3, 2)

// leave only what is odd
#values.filter(calc.odd) \
// create new list of absolute values of list values
#values.map(calc.abs) \
// reverse
#values.rev() \
// convert array of arrays to flat array
#(1, (2, 3)).flatten() \
// join array of string to string
#(("A", "B", "C")
 .join(", ", last: " and "))
Rendered image

List operations

// sum of lists:
#((1, 2, 3) + (4, 5, 6))

// list product:
#((1, 2, 3) * 4)
Rendered image

Empty list

#() \ // this is an empty list
#(1,) \  // this is a list with one element
BAD: #(1) // this is just an element, not a list!
Rendered image

Dictionaries (dict)

Link to Reference.

Dictionaries are objects that store a string "key" and a value, associated with that key.

#let dict = (
  name: "Typst",
  born: 2019,
)

#dict.name \
#(dict.launch = 20)
#dict.len() \
#dict.keys() \
#dict.values() \
#dict.at("born") \
#dict.insert("city", "Berlin ")
#("name" in dict)
Rendered image

Empty dictionary

This is an empty list: #() \
This is an empty dict: #(:)
Rendered image