Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Standard library

The standard library is built into the compiler. There is no import and no package to install. Every function is called through receiver sugar: xs.map(…), s.len(), m.get(k), o.is_some().

The qualified forms (list.map(xs, …), str.len(s)) are not available. The only qualified paths are map.empty(), set.empty(), math.*, and expect.*.

Universal

CallResult
value.to_string()Str for every value with Display
to_string(value) equivalentuses a user-defined to_string(receiver) when the type declares one, including inside ${…}

Str

MethodResult
len()Int, number of Unicode code points
trim(), trim_indent()Str
lower(), upper()Str
split(sep)List<Str>
starts_with(prefix), ends_with(suffix), contains(needle)Bool
replace(from, to)Str
slice(start, end)Str, code-point indices
chars()List<Str>
parse_int()Option<Int>
parse_float()Option<Float>

List<T>

MethodResult
len()Int
get(index)T?
push(value)List<T>
pop()T?
map(fn)List<U>
filter(fn)List<T>
fold(initial, fn)accumulator type
any(fn), all(fn)Bool
sort(), reverse()List<T>
sort_by(fn)List<T>
enumerate()List<Pair<Int, T>>
zip(other)List<Pair<T, U>>
concat(other)List<T>
contains(value)Bool
index_of(value)Int?
join(sep)Str, only for List<Str>

Map<K, V> and Set<T>

Map is ordered by key. Build one from map.empty(); there is no literal.

Map<K, V>Result
len()Int
get(key)V?
insert(key, value)Map<K, V>
remove(key)Map<K, V>
keys()List<K>
values()List<V>
entries()List<Pair<K, V>>
contains_key(key)Bool

map.from(entries) builds a map from List<Entry<K, V>>. Entry comes from the prelude.

Set<T>Result
len()Int
insert(value)Set<T>
remove(value)Set<T>
contains(value)Bool
union(other), intersect(other), difference(other)Set<T>

Set iterates in sorted order. Build one from set.empty().

Dict<V>

Dict has Str keys and preserves insertion order. It is built from a literal: ["a": 1, "b": 2], or [:] for empty.

MethodResult
len()Int
get(key)V?
insert(key, value)Dict<V>
remove(key)Dict<V>
keys()List<Str>
values()List<V>
entries()List<Pair<Str, V>>
contains_key(key)Bool

Equality ignores order; display and iteration follow insertion order. Inserting an existing key keeps its position and replaces the value.

Option<T> and Result<T, E>

Option<T>Result
is_some(), is_none()Bool
unwrap()T, traps on None
unwrap_or(default)T
map(fn)Option<U>
and_then(fn)Option<U>; the lambda must return an Option
Result<T, E>Result
is_ok(), is_err()Bool
unwrap()T, traps on Err
unwrap_or(default)T
map(fn)Result<U, E>
map_err(fn)Result<T, F>
and_then(fn)Result<U, E>; the lambda must return a Result

math

All functions take and return Float where applicable:

math.abs, math.min, math.max, math.floor, math.ceil, math.round, math.sqrt, math.pow, math.pi.

json

CallResult
json.parse(text)Result<Value, Str>
json.stringify(value)Str
json.pretty(value, indent)Str

json.parse reads integral numbers as Value.Int, other numbers as Value.Float, and objects as Value.Object(Dict<Value>), preserving key order. The last duplicate key wins. Trailing garbage and nesting deeper than 128 are errors.

json.stringify writes compact JSON, follows Dict key order, prints integral floats with .0, and leaves non-ASCII text as-is.

Value is the prelude enum:

enum Value {
  Null
  Bool(Bool)
  Int(Int)
  Float(Float)
  Str(Str)
  Array(List<Value>)
  Object(Dict<Value>)
}

ord

ord.cmp(a, b) returns Ordering. The Ordering type is used internally by sort, Map, and Set, but it is not exposed on the language surface yet: Ordering is rejected as a type and Ordering.Less is not a value.

expect

Only valid inside a test block:

CallMeaning
expect.eq(a, b)values are equal
expect.not_equal(a, b)values differ
expect.true(x)value is true
expect.false(x)value is false

Complete example

package list_query {
  export fn index_of(xs: List<Int>, needle: Int) -> Option<Int> {
    xs.index_of(needle)
  }

  export fn at(xs: List<Int>, index: Int) -> Option<Int> {
    xs.get(index)
  }

  export fn has_big(xs: List<Int>) -> Bool {
    xs.any(fn(x) => x > 1)
  }

  test "contains and index" {
    expect.true([1, 2, 3].contains(2))
    expect.eq(index_of([1, 2, 3], 3), Some(2))
  }

  test "safe get" {
    expect.eq(at([1, 2], 1), Some(2))
    expect.eq(at([1, 2], 9), None)
  }

  test "any and all" {
    expect.true(has_big([1, 2]))
    expect.false([1, 2].all(fn(x) => x > 1))
  }
}