Library reference

The library is called on a value: value.name(arguments). The names exist only after a ., so they never take a name from your program, and the language keeps no reserved words. Every result is a new value: nothing changes the text or the array it is called on. Only what a program uses goes into its executable. The full reference is §21 of SPEC.md.

  1. Text (STRING)
  2. Arrays
  3. Queries on tables
  4. JSON
  5. Maps
  6. Input, output and files
  7. Conversions
  8. The standard library (std:)

Text (STRING)

words = "  pear, apple, fig  ".trim().split(", ");   // ["pear", "apple", "fig"]
>> words.sort().join(" < ");                        // apple < fig < pear
n = "42".int() || 0;                                // "4x".int() is _
CallResult
s.find(t), s.find(t, from)INT?: the first position of t (from from), or _
s.contains(t)BOOL: t appears in s
s.starts_with(t), s.ends_with(t)BOOL
s.slice(a), s.slice(a, b)the bytes a … b - 1 (to the end without b); negative counts from the end
s.split(sep)ARRAY of STRING: the parts between each sep ("" gives single bytes)
s.replace(a, b)every a replaced by b
s.trim()without spaces, tabs and line breaks at both ends
s.upper(), s.lower()ASCII and Latin-1 letters changed ("ação".upper() is "AÇÃO")
s.repeat(n)s repeated n times
s.int(), s.float()INT? / FLOAT?: the number in s, or _ if it is not one
#s, s[i], a + blength in bytes, the byte at i (an INT), joining

Arrays

For ARRAYs of INT, FLOAT or STRING. [0 ; n] makes n copies of a value; a + b joins two arrays.

CallResult
v.sort()a new ARRAY in increasing order (stable, n log n; STRING byte by byte)
v.find(x)INT?: the first position of x, or _
v.contains(x)BOOL
v.reverse()a new ARRAY, last element first
v.sum(), v.min(), v.max()INT and FLOAT only: the total (0 for none); the smallest and largest as optionals
parts.join(sep)ARRAY of STRING only: one STRING with sep between the parts

Queries on tables

On an ARRAY of records the argument is an expression of the fields: inside it, the model's field names stand for the fields of each record in turn. It can also use any other name of the program.

rows = PRODUCT @ "products.dat";
cheap = rows.where(price <= limit && active);
best? = rows.sort(-price).first(active);           // the most expensive active one
total = rows.where(active).sum(price);
CallResult
rows.where(cond)a new ARRAY with the records for which cond is true, in order
rows.count(cond)INT: how many
rows.first(cond)the first one, as an optional
rows.sort(key)a new ARRAY in increasing order of key (-key for a number: decreasing)
rows.sum(expr)the total of an INT or FLOAT expression
$rows, $recordthe records as table lines

Tables on disk (a data model is NAME = @[#key: INT(4); name: STRING(12); …];):

WriteMeaning
NAME @ paththe whole table: an ARRAY of NAME
NAME @{k} path, NAME @?{k} pathone record by its key (through a hash index on disk); @? gives _ when it is not there
@{}>> path, r;writes r under its key: replaces it, or adds it
NAME @{k} path = _;deletes the record with key k
NAME @[i] path, @[i]>> path, r;record number i, read or changed in place

JSON

A JSON value stays text: each call finds a part of it and gives that part's text. Numbers become INT or FLOAT with .int() and .float().

name = (item.json("name") || "\"\"").json_text() || "";
price = (item.json("price") || "0").float() || 0.0;
tags = (item.json("tags") || "[]").json_items();
>> name.json_quote();                                // back to a JSON string
CallResult
s.json(key)STRING?: the text of the value of key in the object s
s.json_items()ARRAY of STRING: the items of the array s
s.json_keys(), s.json_values()the keys and the values of the object s, in order
s.json_text()STRING?: the decoded text of the JSON string s
s.json_quote()s as a JSON string, with quotes and escapes
x.to_json(), NAME.from_json(text)any value (records included) to JSON, and a record from a JSON object

Maps

age = {"ana": 30, "bob": 25};          // the types come from the first pair
sales = {0: 0.0 ; 100000};             // empty, with room for 100,000 keys
sales[k] = (sales[k] || 0.0) + q;      // m[k] is an optional
age["bob"] = _;                        // deletes
CallResult
m[k]the value, as an optional (_ when the key is not there)
m[k] = v;, m[k] = _;adds or replaces; deletes
#mhow many keys
m.keys(), m.values()new ARRAYs, in the table's order (sort them when the order matters)
m.contains(k)BOOL

Input, output and files

WriteMeaning
>> x;, !>> x;writes to the standard output, or to the standard error (no line break is added)
<< x;reads a line into x, converted to its type
>>> b;, <<< b;one raw byte out, or in (-1 at the end)
@ path, @? patha whole file as text; @? gives _ if it cannot be read
@>> path, text;replaces a file atomically (a crash leaves the old one or the new one)
@+>> path, text;appends to a file
:: code;ends the program, from anywhere, with an exit code
... ms;waits ms milliseconds

The library also has directories and processes, a TCP server and HTTP (server and client), and protected calls that turn a runtime error into _: see §23–§25 of the SPEC.

Conversions

WriteMeaning
$xthe text of a number, a BOOL or records
$bytesa STRING from an ARRAY of INT (one byte each): $[72, 105] is "Hi"
.nINT to FLOAT: price * .count
#fFLOAT to INT, toward zero
s.int(), s.float()text to number, as an optional
x || fallbackthe value of an optional, or the fallback

The standard library (std:)

Libraries that ship inside the compiler, wherever your program is:

IncludeGives
@ "std:env.nihil";env(key, default), env_int(key, default): the .env file of the current folder, and over it the process's environment
@ "std:json.nihil";json_bool(b), json_text(t), json_text_or_null(t): helpers for writing JSON
@ "std:time.nihil";now_us(), now_ms(): microseconds and milliseconds on the monotonic clock, to time something (subtract two readings); unix_time(): seconds since 1970 by the system clock
@ "std:mcp.nihil";an MCP server over stdio for AI agents: define mcp_tools() and mcp_call(name, arguments), then call mcp_serve(name, version); arg_int and arg_text read the arguments. tools("prefix") builds the tool list from your methods