Lynxer

Documentation

Tuples

A tuple is an immutable, ordered, fixed-length sequence. tuple is a first-class type keyword: it participates in the same checking, conversion, and comparison rules as int, str, or list.

Tuples are always available — no import is needed. The typing module adds higher-level helpers that compose the built-ins (see typing).

Declaration

Each element uses an explicit type, exactly like a list element:

tuple point  = (int 10, int 20);
tuple rgb    = (int 255, int 128, int 0);
tuple mixed  = (str "hello", int 42, bool true);
tuple single = (int 99,);   // single-element tuple — prints as (99,)
tuple empty  = ();          // empty tuple

The trailing comma in (int 99,) is what distinguishes a single-element tuple from a parenthesised expression.

() is the canonical form for both declaration and rebinding. A tuple target also accepts a bracket literal for compatibility with the original:

tuple t = [int 1, int 2];   // the list is coerced to a tuple, (1, 2)
t = [int 3, int 4];         // rebinding works the same way

The bracket form is deprecated and emits a warning (once per source location): Warning: ... tuple assigned from a bracketed list is deprecated; use the '(int 1, int 2)' form instead. suppressDeprecationWarning() silences it.

Prefer () in new code; tupleCreate(...) and listToTuple(...) remain the explicit constructors.

You can also build a tuple dynamically with tupleCreate(...), which infers each element's type from the argument:

tuple dyn = tupleCreate(1, "a", true);   // (1, a, true)
tuple none = tupleCreate();              // ()

Type system

tuple coords = (int 0, int 0);
println(returnType(coords));    // tuple
println(returnLength(coords));  // 2
println(tupleLen(coords));      // 2

Built-in functions

FunctionSignatureReturnsDescription
tupleCreate(v1, v2, ...)tupleBuilds a tuple, inferring element types
tupleGet(tuple t, int idx)elementElement at idx; negative indices count from the end
tupleLen(tuple t)intNumber of elements
tupleContains(tuple t, any val)boolMembership test
contains(tuple t, any val)boolMembership test shared with lists
tupleIndex(tuple t, any val)intFirst index of val, or -1
tupleCount(tuple t, any val)intOccurrences of val
tupleFirst / tupleLast(tuple t)elementFirst / last element (error when empty)
tupleSlice(tuple t, int start, int stop)tupleSub-tuple [start, stop)
tupleConcat(tuple a, tuple b)tupleConcatenation
tupleReverse(tuple t)tupleElements reversed
tupleSort / tupleSortDesc(tuple t)tupleSorted ascending / descending
tupleMin / tupleMax(tuple t)elementMinimum / maximum element
tupleSum / tupleMean(tuple t)floatSum / arithmetic mean of numeric elements
tupleAny / tupleAll(tuple t)boolAny / all elements truthy
tupleUnique(tuple t)tupleDuplicates removed, order preserved
tupleFlatten(tuple t)tupleOne-level flatten of nested tuples
tupleZip(tuple a, tuple b)tuplePairwise JSON strings {"a": 1, "b": 2}
tupleJoin(tuple t, str sep)strJoins elements with sep (via strOf)
tupleJsonArray(tuple t)strJSON array string, e.g. "[1, 2]"
tupleToList(tuple t)listConverts to a mutable list
listToTuple(list l)tupleConverts a list to a tuple
global setup(){}

global main(){
    tuple t = (int 10, int 20, int 30, int 20);

    println(tupleLen(t));            // 4
    println(tupleGet(t, -1));        // 20
    println(tupleSlice(t, 1, 3));    // (20, 30)
    println(tupleConcat(t, tupleCreate(40)));   // (10, 20, 30, 20, 40)
    println(tupleJsonArray(t));      // [10, 20, 30, 20]
    println(tupleJoin(t, "-"));      // 10-20-30-20
    println(tupleToList(t));         // [10, 20, 30, 20]
}

Equality and comparison

is (isnt) compares tuples structurally — same length and equal elements:

println((int 1, int 2) is (int 1, int 2));     // true
println((int 1, int 2) isnt (int 1, int 9));   // true

Tuples have no ordering operators; use tupleToList() + sortList() when you need a sorted order.

Iteration

There is no for ... in form. Iterate with a C-style for over the element count, or convert to a list first:

tuple t = (int 10, int 20, int 30);

for (int i = 0; i < tupleLen(t)) {     // the update `i = i + 1` is implicit
    println(tupleGet(t, i));
}

list values = tupleToList(t);
for (int i = 0; i < returnLength(values)) {
    println(listGet(values, i));
}

Differences from the original Lynxer

See also