Builtins

generated from midad spec --json at build time; not edited by hand

72 builtins, each with every parameter set it accepts and the type it returns. A | separates the types one argument may take.

core

len(list|string|map) -> number

The number of elements, characters or keys.

read_file(string) -> option

Reads a file as text; None when it cannot be read. Needs fs_read.

write_file(string, string) -> boolean

Writes text to a file and answers whether it worked. Needs fs_write.

  • write_file(string) -> boolean
  • write_file(string) -> boolean
http_get(string) -> option

Fetches a URL and answers the body as text, or None. Needs net.

json_get(string, string) -> option

Reads one key out of JSON text; None when the text or the key is missing.

  • json_get(string) -> option
  • json_get(string) -> option
html(string) -> string

Renders an html:NAME template, optionally binding its holes from a map.

str(number|float|string|boolean|list|map|option|result|any) -> string

Any value as a string; lists and maps come out as compact JSON.

int(number|float|string) -> number

A number from a number, a float or a whole-number string. A bad string is a runtime error.

int_or(string, number) -> number

A number from a string, or the fallback when it cannot be parsed.

  • int_or(string) -> number
  • int_or(number) -> number
float_or(string, float|number) -> float

A float from a string, or the fallback.

  • float_or(string) -> float
  • float_or(float, number) -> float
float(number|float|string) -> float

A float from a number, a float or a numeric string.

print(number|float|string|boolean|list|map|option|result|any) -> void

Writes a value and a newline to standard output.

push(list, number|float|string|boolean|list|map|option|result|any) -> void

Changes its first argument in place and answers nothing, so the call is a statement on its own; using its result is E0260.

Adds one element to the end of the list, in place; the call answers nothing.

  • push(list) -> void
  • push(number, float, string, boolean, list, map, option, result, any) -> void
at(list, number) -> any

The element at an index, as any. A bad index is a runtime error.

  • at(list) -> any
  • at(number) -> any
idiv(number, number) -> number

Integer division, floored: idiv(-7, 2) is -4. A zero denominator is a runtime error.

  • idiv(number) -> number
  • idiv(number) -> number
set_at(list, number, number|float|string|boolean|list|map|option|result|any) -> void

Changes its first argument in place and answers nothing, so the call is a statement on its own; using its result is E0260.

Replaces the element at an index, in place; the call answers nothing.

  • set_at(list) -> void
  • set_at(number) -> void
  • set_at(number, float, string, boolean, list, map, option, result, any) -> void
remove_at(list, number) -> any

Changes its first argument in place, and answers the element it removed.

Removes the element at an index, in place, and answers the element it removed.

  • remove_at(list) -> any
  • remove_at(number) -> any
get(map, string) -> any

The value for a key, as any; None when the key is absent.

  • get(map) -> any
  • get(string) -> any
has(map, string) -> boolean

Whether a map has a key.

  • has(map) -> boolean
  • has(string) -> boolean
put(map, string, number|float|string|boolean|list|map|option|result|any) -> map

Changes its first argument in place, and answers the element it removed.

A new map with a key set to a value.

  • put(map) -> map
  • put(string) -> map
  • put(number, float, string, boolean, list, map, option, result, any) -> map
del(map, string) -> map

Changes its first argument in place, and answers the element it removed.

A new map without a key.

  • del(map) -> map
  • del(string) -> map
keys(map) -> list

The keys of a map, in byte order.

Some(number|float|string|boolean|list|map|option|result|any) -> option

Wraps a value in a non-empty option.

Ok(number|float|string|boolean|list|map|option|result|any) -> result

Wraps a value in a successful result.

Err(number|float|string|boolean|list|map|option|result|any) -> result

Wraps a reason in a failed result.

is_some(option) -> boolean

Whether an option holds a value.

is_none(option) -> boolean

Whether an option is empty.

unwrap_or(option|result, number|float|string|boolean|list|map|option|result|any) -> any

The value inside an option or result, or the fallback.

  • unwrap_or(option, result) -> any
  • unwrap_or(number, float, string, boolean, list, map, option, result, any) -> any
is_ok(result) -> boolean

Whether a result is Ok.

is_err(result) -> boolean

Whether a result is Err.

respond(number, number|float|string|boolean|list|map|option|result|any, number|float|string|boolean|list|map|option|result|any) -> map

A response for a route: status 100 to 599, a body, and optionally headers.

  • respond(number) -> map
  • respond(number, float, string, boolean, list, map, option, result, any) -> map
  • respond(number, float, string, boolean, list, map, option, result, any) -> map
respond_html(number, string) -> map

A response whose body is sent as HTML.

  • respond_html(number) -> map
  • respond_html(string) -> map
eval_source(string) -> map

Runs a string as a program and answers a map with its output. Needs eval.

split(string, string) -> list

Splits a string on a non-empty separator.

  • split(string) -> list
  • split(string) -> list
trim(string) -> string

The string without leading and trailing whitespace.

contains(string|list, number|float|string|boolean|list|map|option|result|any) -> boolean

Whether a string contains a substring, or a list an element.

  • contains(string, list) -> boolean
  • contains(number, float, string, boolean, list, map, option, result, any) -> boolean
is_null(number|float|string|boolean|list|map|option|result|any) -> boolean

Whether a value is null: a SQL NULL from db.query, a JSON null, or the null literal. Not true of None.

type_of(number|float|string|boolean|list|map|option|result|any) -> string

The runtime kind: int, float, string, bool, list, map, option, result, null.

chars(string) -> list

A list of one-character strings.

join(list, string) -> string

Joins a list of strings with a separator.

  • join(list) -> string
  • join(string) -> string
upper(string) -> string

The string in upper case. A native C build refuses it.

lower(string) -> string

The string in lower case. A native C build refuses it.

replace(string, string, string) -> string

Every occurrence of a substring replaced with another.

  • replace(string) -> string
  • replace(string) -> string
  • replace(string) -> string
substr(string, number, number) -> string

A slice: start index and length.

  • substr(string) -> string
  • substr(number) -> string
  • substr(number) -> string
sort(list) -> void

Changes its first argument in place and answers nothing, so the call is a statement on its own; using its result is E0260.

Sorts the list in place; the call answers nothing.

starts_with(string, string) -> boolean

Whether a string starts with a prefix.

  • starts_with(string) -> boolean
  • starts_with(string) -> boolean

int builtins

int.parse(string) -> option

A number as an option: None for anything that is not a whole number.

float builtins

float.parse(string) -> option

A float as an option: None for anything that is not a number.

json builtins

json.parse(string) -> any

Parses JSON text into a value; None when the text is not JSON.

json.at(number|float|string|boolean|list|map|option|result|any, string) -> any

Reads one key out of a parsed JSON value.

  • json.at(number, float, string, boolean, list, map, option, result, any) -> any
  • json.at(string) -> any
json.stringify(number|float|string|boolean|list|map|option|result|any) -> string

A value as JSON text.

time builtins

time.now() -> number

The current time in milliseconds since the epoch.

time.iso(number) -> string

An ISO-8601 timestamp with milliseconds, in UTC.

time.date(number) -> string

The date part, YYYY-MM-DD.

time.parse(string) -> option

A timestamp string as an option of milliseconds.

time.add_days(number, number) -> number

A time in milliseconds moved by a number of days.

  • time.add_days(number) -> number
  • time.add_days(number) -> number

fs builtins

fs.read(string) -> string

Reads a file as text and fails with a runtime error when it cannot. Needs fs_read.

fs.write(string, string) -> boolean

Writes text to a file. Needs fs_write.

  • fs.write(string) -> boolean
  • fs.write(string) -> boolean

env builtins

env.get(string) -> string

An environment variable as a string. Needs env.

crypto builtins

crypto.sha256(string) -> string

The SHA-256 of a string, as hex.

crypto.hmac_sha256(string, string) -> string

The HMAC-SHA256 of a message under a key, as hex.

  • crypto.hmac_sha256(string) -> string
  • crypto.hmac_sha256(string) -> string
crypto.pbkdf2_sha256(string, string, number, number) -> string

PBKDF2-HMAC-SHA256 from a password, a salt, an iteration count and a key length.

  • crypto.pbkdf2_sha256(string) -> string
  • crypto.pbkdf2_sha256(string) -> string
  • crypto.pbkdf2_sha256(number) -> string
  • crypto.pbkdf2_sha256(number) -> string

hex builtins

hex.encode(string) -> string

The bytes of a string as hex.

hex.decode(string) -> option

Hex as a string, or None when it is not valid hex.

random builtins

random.hex(number) -> string

Random bytes as hex: the argument is how many bytes.

db builtins

db.open(string) -> number

Opens a SQLite database and answers a connection. Needs db.

db.close(number) -> any

Closes a connection.

db.query(number, string, list) -> list

Runs a query with bound parameters and answers a list of row maps, at most 10,000.

  • db.query(number) -> list
  • db.query(string) -> list
  • db.query(list) -> list
db.exec(number, string, list) -> number

Runs a statement with bound parameters and answers the affected row count.

  • db.exec(number) -> number
  • db.exec(string) -> number
  • db.exec(list) -> number
db.begin(number) -> any

Opens a transaction on a connection.

db.commit(number) -> any

Commits the open transaction.

db.rollback(number) -> any

Rolls the open transaction back.

On this page