Skip to content

Built-in Functions

Complete reference for all built-in functions available in Qosm.


These functions are available in every Qosm program without any imports or declarations.

Array Functions

| Function | Type | Description | |----------|------|-------------| | map | ('a -> 'b ! 'e) -> Array<'a> -> Array<'b> ! 'e | Apply a function to each element | | filter | ('a -> Bool ! 'e) -> Array<'a> -> Array<'a> ! 'e | Keep elements that satisfy a predicate | | foldl | ('b -> 'a -> 'b ! 'e) -> 'b -> Array<'a> -> 'b ! 'e | Left fold with accumulator | | length | Array<'a> -> Int | Number of elements | | head | Array<'a> -> Option<'a> | First element (or None) | | tail | Array<'a> -> Array<'a> | All elements except the first | | append | Array<'a> -> Array<'a> -> Array<'a> | Concatenate two arrays | | nth | Array<'a> -> Int -> Option<'a> | Element at index (or None) | | take | Int -> Array<'a> -> Array<'a> | First N elements | | sort | Array<'a> -> Array<'a> | Sort ascending | | sort_by | ('a -> 'b) -> Array<'a> -> Array<'a> | Sort ascending by key function |

Examples

// Map: transform each element
let doubled = map (λ x -> x * 2) [1, 2, 3];
// [2, 4, 6]

// Filter: keep matching elements
let evens = filter (λ x -> x % 2 == 0) [1, 2, 3, 4];
// [2, 4]

// Foldl: reduce to a single value
let sum = foldl (λ acc x -> acc + x) 0 [1, 2, 3];
// 6

// Sort by key (descending: negate the key)
let by_age = sort_by (λ p -> 0 - .age p) people;

// Head and tail
match head [1, 2, 3] with (
| Some{value: x} -> x    // 1
| None -> 0);

map, filter, and foldl are effect-polymorphic — you can pass effectful callbacks and the effects propagate correctly. For example, map (λ x -> my_model @{...} \...`) itemsworks and tracksLLM.Call`.

Result Functions

Result<'ok, 'err> has two constructors: Ok{value: v} and Err{value: e}.

| Function | Type | Description | |----------|------|-------------| | and_then | ('a -> Result<'b, 'e>) -> Result<'a, 'e> -> Result<'b, 'e> | Chain fallible operations | | map_result | ('a -> 'b) -> Result<'a, 'e> -> Result<'b, 'e> | Transform the Ok value | | map_err | ('e1 -> 'e2) -> Result<'a, 'e1> -> Result<'a, 'e2> | Transform the Err value | | unwrap_result | 'a -> Result<'a, 'e> -> 'a | Get Ok value or default | | result_of_option | 'e -> Option<'a> -> Result<'a, 'e> | Convert Option to Result |

Examples

// Chain two fallible operations
let pipeline input =
  my_model @{text: String} `Extract: {{ input }}`
  |> and_then (λ r ->
    parse_json @{name: String} (.text r));

// Map over the Ok value
let uppercased =
  my_model @{text: String} `...`
  |> map_result (λ r -> { text: .text r ++ "!" });

// Unwrap with a default
let name = unwrap_result "unknown" (Ok{value: "Alice"});
// "Alice"

Option Functions

Option<'a> has two constructors: Some{value: v} and None.

| Function | Type | Description | |----------|------|-------------| | map_opt | ('a -> 'b) -> Option<'a> -> Option<'b> | Transform the Some value | | unwrap_or | 'a -> Option<'a> -> 'a | Get Some value or default | | is_some | Option<'a> -> Bool | True if Some | | is_none | Option<'a> -> Bool | True if None |

Examples

let first = head [1, 2, 3];
let doubled = map_opt (λ x -> x * 2) first;
// Some{value: 2}

let val = unwrap_or 0 (nth [10, 20, 30] 5);
// 0 (index out of bounds)

Conversion Functions

| Function | Type | Description | |----------|------|-------------| | to_string | 'a -> String | Convert any value to a readable string | | to_json | 'a -> String | Convert any value to a JSON string | | parse_json | String -> Result<'a, QosmError> | Parse a JSON string into a typed value |

Examples

to_string 42           // "42"
to_string true         // "true"
to_json {name: "Alice"}  // "{\"name\":\"Alice\"}"

// parse_json requires @Type to know the target type
parse_json @{name: String, age: Int} """{"name": "Alice", "age": 30}"""
// Ok{value: {name: "Alice", age: 30}}

parse_json requires a @Type annotation: parse_json @{name: String} str. Without it, the parser doesn't know what type to produce.

Utility Functions

| Function | Type | Description | |----------|------|-------------| | id | 'a -> 'a | Identity function | | const | 'a -> 'b -> 'a | Always returns first argument | | flip | ('a -> 'b -> 'c) -> 'b -> 'a -> 'c | Swap argument order | | \|> | 'a -> ('a -> 'b) -> 'b | Pipe (left to right) | | >> | ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c) | Forward composition | | << | ('b -> 'c) -> ('a -> 'b) -> ('a -> 'c) | Backward composition |

Pair Functions

Pair<'a, 'b> is constructed with Pair{fst: a, snd: b}.

| Function | Type | Description | |----------|------|-------------| | fst | Pair<'a, 'b> -> 'a | First element | | snd | Pair<'a, 'b> -> 'b | Second element | | swap | Pair<'a, 'b> -> Pair<'b, 'a> | Swap elements |