Built-in Functions
All built-in functions available in Osprey.
Channel
Signature: Channel(capacity: int) -> Channel<t0>
Creates a buffered channel with a positive capacity.
List
Signature: List() -> List<t0>
Creates a new empty list.
Map
Signature: Map() -> Map<string, t0>
Creates a new empty string-keyed map.
abs
Signature: abs(value: int) -> Result<int, MathError>
Returns Result<int, MathError>. INT64_MIN yields Error because its positive magnitude is not representable.
await
Signature: await(fiber: Fiber<t0>) -> t0
Waits for a fiber to finish and returns its result, suspending the current fiber until then.
awaitProcess
Signature: awaitProcess(handle: int) -> int
Waits for a spawned process to complete and returns its exit code. Blocks until the process finishes.
byteAt
Signature: byteAt(text: string, index: int) -> Result<int, Error>
Returns the byte at the given index (0-255), or an error if the index is out of range.
byteLength
Signature: byteLength(text: string) -> int
Returns the number of bytes in the string's UTF-8 encoding.
check
Signature: check(label: string, expected: any, actual: any) -> Unit
Asserts expected equals actual and includes label in a mismatch diagnostic. Execution continues after a mismatch.
checkAll
Signature: checkAll(label: string, conditions: List<bool>) -> Unit
Runs every boolean in a list literal as an independent labeled soft assertion. All conditions run even when an earlier one fails.
checkFalse
Signature: checkFalse(label: string, actual: bool) -> Unit
Labeled assertion that a boolean expression is false.
checkTrue
Signature: checkTrue(label: string, actual: bool) -> Unit
Labeled assertion that a boolean expression is true.
checkedAdd
Signature: checkedAdd(a: int, b: int) -> Result<int, Error>
Named overflow-checked integer addition, returning Result<int, Error>.
checkedMul
Signature: checkedMul(a: int, b: int) -> Result<int, Error>
Named overflow-checked integer multiplication, returning Result<int, Error>.
checkedSub
Signature: checkedSub(a: int, b: int) -> Result<int, Error>
Named overflow-checked integer subtraction, returning Result<int, Error>.
cleanupProcess
Signature: cleanupProcess(handle: int) -> Unit
Cleans up resources associated with a completed process. Should be called after awaitProcess.
codePointAt
Signature: codePointAt(text: string, index: int) -> Result<int, Error>
Returns the Unicode code point that begins at the given byte index. Fails on an invalid index or malformed UTF-8.
codePointWidth
Signature: codePointWidth(codePoint: int) -> Result<int, Error>
Returns how many bytes the given Unicode code point occupies in UTF-8 (1-4).
contains
Signature: contains(s: string, needle: string) -> bool
True if needle appears anywhere in s. Empty needle returns true.
drop
Signature: drop(s: string, n: int) -> string
Returns s without its first n bytes. Clamps; never fails.
endsWith
Signature: endsWith(s: string, suffix: string) -> bool
True if s ends with suffix.
expect
Signature: expect(actual: any, expected: any) -> Unit
Asserts two values are equal by canonical rendering. Success payloads compare by value; Errors remain visible as Error(message). On mismatch, marks the enclosing test failed and prints a diagnostic; execution continues.
expectAll
Signature: expectAll(conditions: List<bool>) -> Unit
Runs every boolean in a list literal as an independent soft assertion. All conditions run even when an earlier one fails.
expectFalse
Signature: expectFalse(actual: bool) -> Unit
Asserts that a boolean expression is false.
expectTrue
Signature: expectTrue(actual: bool) -> Unit
Asserts that a boolean expression is true.
fiberDone
Signature: fiberDone(fiber: Fiber<t0>) -> int
Returns 1 if the given fiber has finished, 0 otherwise.
fiber_yield
Signature: fiber_yield(value: int) -> int
Yields control to the fiber scheduler and returns its integer value.
filter
Signature: filter(iterator: Iterator<t0>, predicate: (t0) -> bool) -> Iterator<t0>
Filters elements in an iterator based on a predicate function.
fold
Signature: fold(iterator: Iterator<t0>, initial: t1, fn: (t1, t0) -> t1) -> t1
Reduces an iterator to a single value by repeatedly applying a function.
forEach
Signature: forEach(iterator: Iterator<t0>, function: (t0) -> Unit) -> Unit
Applies a function to each element in an iterator.
forEachList
Signature: forEachList(list: List<t0>, function: (t0) -> Unit) -> Unit
Applies function to every list element in index order.
fromCodePoint
Signature: fromCodePoint(codePoint: int) -> Result<string, Error>
Returns the single-character string for a nonzero Unicode scalar, or an error for U+0000 and non-scalars.
httpCloseClient
Signature: httpCloseClient(clientID: int) -> int
Closes the HTTP client and returns the runtime status.
httpCreateClient
Signature: httpCreateClient(base_url: string, timeout: int) -> int
Creates an HTTP client and returns its handle, or a negative runtime error.
httpCreateServer
Signature: httpCreateServer(port: int, address: string) -> int
Creates an HTTP server bound to the specified port and address.
httpDelete
Signature: httpDelete(clientID: int, path: string, headers: string) -> int
Makes an HTTP DELETE request and returns its status code, or a negative transport error.
httpGet
Signature: httpGet(clientID: int, path: string, headers: string) -> int
Makes an HTTP GET request and returns its status code, or a negative transport error.
httpGetResponse
Signature: httpGetResponse(clientID: int, path: string, headers: string) -> Result<int, Error>
Sends an HTTP GET request and returns a response handle for inspecting the status, headers, and body.
httpListen
Signature: httpListen(serverID: int, handler: (string, string, string, string) -> HttpResponse) -> int
Starts the HTTP server with a request handler and returns 0 or a negative runtime error.
httpPost
Signature: httpPost(clientID: int, path: string, body: string, headers: string) -> int
Makes an HTTP POST request and returns its status code, or a negative transport error.
httpPut
Signature: httpPut(clientID: int, path: string, body: string, headers: string) -> int
Makes an HTTP PUT request and returns its status code, or a negative transport error.
httpResponseBody
Signature: httpResponseBody(responseID: int) -> Result<string, Error>
Returns the body of a response handle as a string.
httpResponseFree
Signature: httpResponseFree(responseID: int) -> Result<int, Error>
Releases a response handle; an invalid handle or double free returns Error.
httpResponseHeader
Signature: httpResponseHeader(responseID: int, name: string) -> Result<string, Error>
Returns the value of the named header from a response handle.
httpResponseStatus
Signature: httpResponseStatus(responseID: int) -> int
Returns the HTTP status code of a response handle.
httpStopServer
Signature: httpStopServer(serverID: int) -> int
Stops the HTTP server and returns the runtime status.
indexOf
Signature: indexOf(s: string, needle: string) -> Result<int, Error>
Returns the byte index of needle's first occurrence, or Error with 'indexOf: substring not found'.
input
Signature: input() -> string
Reads a string from the user's input.
intDiv
Signature: intDiv(a: int, b: int) -> Result<int, Error>
Truncating integer division. Zero returns Error(division by zero); INT64_MIN / -1 returns Error(integer overflow).
isEmpty
Signature: isEmpty(s: string | List<T> | Map<string, V>) -> bool
True if a string has zero bytes or a List/Map has zero elements.
join
Signature: join(parts: List<string>, separator: string) -> string
Concatenates parts with separator between each pair.
jsonFree
Signature: jsonFree(document: int) -> Result<int, Error>
Releases a parsed JSON document handle obtained from jsonParse.
jsonGet
Signature: jsonGet(document: int, path: string) -> Result<string, Error>
Returns a JSON scalar at the given path as a string. Arrays, objects, invalid paths, and invalid handles return Error.
jsonLength
Signature: jsonLength(document: int, path: string) -> int
Returns an array length or object member count at the given path, or -1 for an invalid path, handle, or scalar.
jsonParse
Signature: jsonParse(text: string) -> Result<int, Error>
Parses a JSON string and returns an opaque document handle for querying, or an error on malformed input.
length
Signature: length(s: string | List<T> | Map<string, V>) -> int
Returns a string's byte length or a List/Map element count.
lines
Signature: lines(s: string) -> List<string>
Splits on '\n'. A trailing newline does not produce an empty entry.
listAppend
Signature: listAppend(list: List<t0>, value: t0) -> List<t0>
Returns a new list with value at the end. Amortized O(1).
listConcat
Signature: listConcat(left: List<t0>, right: List<t0>) -> List<t0>
Returns left ++ right. Same as left + right.
listContains
Signature: listContains(list: List<t0>, value: t0) -> bool
Linear search. Strings compare by content, scalars by value, and managed handles by identity.
listGet
Signature: listGet(list: List<t0>, index: int) -> Result<t0, Error>
Returns the element at the given index, or an error if the index is out of range.
listLength
Signature: listLength(list: List<t0>) -> int
Returns the number of elements in a list. O(1).
listPrepend
Signature: listPrepend(list: List<t0>, value: t0) -> List<t0>
Returns a new list with value at the front. O(n).
listReverse
Signature: listReverse(list: List<t0>) -> List<t0>
Returns a new list in reverse order.
map
Signature: map(iterator: Iterator<t0>, fn: (t0) -> t1) -> Iterator<t1>
Transforms each element in an iterator using a function, returning a new iterator.
mapContains
Signature: mapContains(map: Map<string, t0>, key: string) -> bool
True iff key is present in map.
mapGet
Signature: mapGet(map: Map<string, t0>, key: string) -> Result<t0, Error>
Returns the value associated with the key, or an error if the key is absent.
mapKeys
Signature: mapKeys(map: Map<string, t0>) -> List<string>
All keys of the map as a list. Order unspecified.
mapLength
Signature: mapLength(map: Map<string, t0>) -> int
Returns the number of entries in a map. O(1).
mapMerge
Signature: mapMerge(left: Map<string, t0>, right: Map<string, t0>) -> Map<string, t0>
Right-biased union. Same as left + right.
mapRemove
Signature: mapRemove(map: Map<string, t0>, key: string) -> Map<string, t0>
Returns a new map without key. No-op if key is absent.
mapSet
Signature: mapSet(map: Map<string, t0>, key: string, value: t0) -> Map<string, t0>
Returns a new map with key bound to value (replaces prior binding).
mapValues
Signature: mapValues(map: Map<string, t0>) -> List<t0>
All values of the map as a list. Order matches mapKeys.
padEnd
Signature: padEnd(s: string, targetLength: int, fill: string) -> Result<string, Error>
Pads s on the right with copies of fill to reach targetLength bytes.
padStart
Signature: padStart(s: string, targetLength: int, fill: string) -> Result<string, Error>
Pads s on the left with copies of fill to reach targetLength bytes.
parseFloat
Signature: parseFloat(s: string) -> Result<float, Error>
Strict finite base-10 floating-point parser. Rejects surrounding whitespace, NaN/infinity spellings, and hexadecimal floats.
parseInt
Signature: parseInt(s: string) -> Result<int, Error>
Strict base-10 signed-int parser. No whitespace tolerance.
Signature: print(value: int | float | bool | string | Unit | any | Result<printable, printable>) -> Unit
Writes a supported scalar or Result representation followed by a newline. Unit renders as 0.
random
Signature: random() -> int
A cryptographically-secure uniform random non-negative integer (0 .. 2^63-1), drawn fresh from the OS entropy source. Unseeded and unpredictable.
randomBelow
Signature: randomBelow(n: int) -> Result<int, Error>
A cryptographically-secure uniform random integer in [0, n), unbiased by rejection sampling. Returns Result<int, Error> when n is positive and Error otherwise.
range
Signature: range(start: int, end: int) -> Iterator<int>
Creates an iterator that generates numbers from start to end (exclusive).
readFile
Signature: readFile(filename: string) -> Result<string, Error>
Reads the entire contents of a file as a string.
recv
Signature: recv(channel: Channel<t0>) -> t0
Blocks while the channel is empty, then receives its oldest value.
repeat
Signature: repeat(s: string, n: int) -> Result<string, Error>
Concatenates s with itself n times. A negative n returns Error.
replace
Signature: replace(s: string, needle: string, replacement: string) -> Result<string, Error>
Replaces every occurrence of needle. An empty needle returns Error.
reverse
Signature: reverse(s: string) -> string
Reverses byte order.
send
Signature: send(channel: Channel<t0>, value: t0) -> Unit
Blocks while the channel is full, then sends a value and returns Unit.
sleep
Signature: sleep(milliseconds: int) -> Unit
Pauses execution for the specified number of milliseconds.
spawnProcess
Signature: spawnProcess(command: string, callback: (int, int, string) -> Unit) -> Result<int, Error>
Spawns a process and reports stdout, stderr and exit events through the required callback. Returns a handle for the running process.
split
Signature: split(s: string, separator: string) -> Result<List<string>, Error>
Splits s on separator. An empty separator returns Error.
startsWith
Signature: startsWith(s: string, prefix: string) -> bool
True if s begins with prefix.
substring
Signature: substring(s: string, start: int, end: int) -> Result<string, Error>
Extracts s[start, end). Invalid or inverted bounds return Error.
take
Signature: take(s: string, n: int) -> string
Returns at most the first n bytes of s. Clamps; never fails.
termClear
Signature: termClear() -> int
Clears the terminal screen.
termCols
Signature: termCols() -> int
Returns the terminal width in columns.
termHideCursor
Signature: termHideCursor() -> int
Hides the terminal cursor.
termMoveCursor
Signature: termMoveCursor(row: int, col: int) -> int
Moves the terminal cursor to the given row and column.
termRawMode
Signature: termRawMode(enabled: int) -> Unit
Enables (1) or disables (0) raw terminal input mode, so keypresses arrive unbuffered.
termReadKey
Signature: termReadKey() -> Result<string, Error>
Reads a single keypress from the terminal and returns it as a string.
termRows
Signature: termRows() -> int
Returns the terminal height in rows.
termShowCursor
Signature: termShowCursor() -> int
Shows the terminal cursor.
test
Signature: test(name: string, body: () -> t0) -> Unit
Runs body as one named test case and prints a TAP result line. A case fails when any assertion inside it fails; the program exits non-zero if any case failed.
toLowerCase
Signature: toLowerCase(s: string) -> string
ASCII-aware lowercase.
toString
Signature: toString(value: int | float | bool | string | Unit | any | Result<printable, printable>) -> string
Formats the same scalar and Result values accepted by print without writing output. Unit renders as 0.
toUpperCase
Signature: toUpperCase(s: string) -> string
Converts ASCII letters to uppercase; other bytes are unchanged.
trim
Signature: trim(s: string) -> string
Removes leading and trailing whitespace.
trimEnd
Signature: trimEnd(s: string) -> string
Removes trailing whitespace.
trimStart
Signature: trimStart(s: string) -> string
Removes leading whitespace.
websocketClose
Signature: websocketClose(wsID: int) -> int
Closes the WebSocket connection and returns the runtime status.
websocketConnect
Signature: websocketConnect(url: string) -> int
Connects to a WebSocket server at the given URL and returns a connection id.
websocketCreateServer
Signature: websocketCreateServer(port: int, address: string, path: string) -> int
Creates a WebSocket server and returns its handle, or a negative runtime error.
websocketKeepAlive
Signature: websocketKeepAlive() -> Unit
Blocks until SIGINT or SIGTERM so server threads remain alive.
websocketSend
Signature: websocketSend(wsID: int, message: string) -> int
Sends one text frame and returns 0 or a negative runtime error.
websocketServerBroadcast
Signature: websocketServerBroadcast(serverID: int, message: string) -> int
Broadcasts one text frame and returns the number of connections written.
websocketServerListen
Signature: websocketServerListen(serverID: int) -> int
Starts the WebSocket server and returns 0 or a negative runtime error.
words
Signature: words(s: string) -> List<string>
Splits on runs of whitespace; empty results dropped.
writeFile
Signature: writeFile(filename: string, content: string) -> Result<int, Error>
Writes content to a file. Creates the file if it doesn't exist. Returns number of bytes written.
yield
Signature: yield() -> Unit
Yields control from the current fiber, letting other ready fibers run.