List

The List: module provides dynamic resizable arrays with a declared element type. Use Array when you need a fixed-size collection.


Call Effect
List:Number name Create an empty list of numbers
List:String name Create an empty list of strings
List:Boolean name Create an empty list of booleans
List:ClassName name Create a list of class instances
List:Push(val, name) Append a value
List:Pop(name, outVar) Remove and return last element → outVar
List:Get(index, name, outVar) Read element at 0-based index → outVar
List:Set(index, val, name) Write element at 0-based index
List:Size(name, outVar) Number of elements → outVar
List:Remove(index, name) Remove element at index (shifts later elements)
List:Contains(val, name, outVar) true if value found → outVar
List:Clear(name) Remove all elements
List:Sort(name) Sort in-place (numbers numerically, strings lexicographically)
List:Sort(cmpFn, name) Sort in-place with comparator: cmpFn(a, b) : Boolean
List:Filter(predFn, src, dst) Fill dst with elements where predFn(x) : Boolean is true
List:Map(xfmFn, src, dst) Fill dst with xfmFn(x) : Type applied to each element
List:Reduce(redFn, init, src, outVar) Fold: redFn(acc, x) : Type → final accumulator → outVar

dst and outVar must be pre-declared. Filter/Map clear dst before filling it. Callback functions must use typed parameters and declare a return type.

List:Number scores;
List:Push(10, scores);
List:Push(20, scores);
List:Push(30, scores);

Var:Number sz;
List:Size(scores, sz);
Konsol:Print(sz);           // 3

Var:Number val;
List:Get(0, scores, val);
Konsol:Print(val);          // 10

List:Sort(scores);

Var:Boolean found;
List:Contains(20, scores, found);
Konsol:Print(found);        // true

List:Remove(1, scores);
List:Size(scores, sz);
Konsol:Print(sz);           // 2

Examples

List

// 23_list.ks — List module (dynamic array)

Konsol:Print("=== push / size / get ===");
List:Number items;
List:Push(10, items);
List:Push(20, items);
List:Push(30, items);
Var:Number sz;
List:Size(items, sz);
Konsol:Print(sz);         // 3
Var:Number val;
List:Get(0, items, val);
Konsol:Print(val);        // 10
List:Get(2, items, val);
Konsol:Print(val);        // 30

Konsol:Print("=== set ===");
List:Set(1, 99, items);
List:Get(1, items, val);
Konsol:Print(val);        // 99

Konsol:Print("=== pop ===");
List:Pop(items, val);
Konsol:Print(val);        // 30 (was at index 2 before pop)
List:Size(items, sz);
Konsol:Print(sz);         // 2

Konsol:Print("=== contains ===");
Var:Boolean found;
List:Contains(10, items, found);
Konsol:Print(found);      // true
List:Contains(30, items, found);
Konsol:Print(found);      // false

Konsol:Print("=== remove ===");
List:Push(55, items);
List:Push(77, items);
List:Remove(0, items);    // remove first element (10)
List:Get(0, items, val);
Konsol:Print(val);        // 99

Konsol:Print("=== string list ===");
List:String words;
List:Push("alpha", words);
List:Push("beta", words);
List:Push("gamma", words);
List:Size(words, sz);
Konsol:Print(sz);         // 3
Var:String word;
List:Get(1, words, word);
Konsol:Print(word);       // beta

Konsol:Print("=== clear ===");
List:Clear(items);
List:Size(items, sz);
Konsol:Print(sz);         // 0

Konsol:Print("=== list in loop ===");
List:Number nums;
for (Var:Number i = 0; i < 5; i++) {
    List:Push(i * i, nums);
}
List:Size(nums, sz);
Konsol:Print(sz);         // 5
for (Var:Number j = 0; j < 5; j++) {
    List:Get(j, nums, val);
    Konsol:Print(val);    // 0 1 4 9 16
}

Functional list operations

// 34_list_functional.ks — List:Sort / Filter / Map / Reduce

// ── helpers ───────────────────────────────────────────────────────────────────
function descCmp(Number a, Number b) : Boolean {
    return a > b;
}

function isEven(Number n) : Boolean {
    return n % 2 == 0;
}

function doubleIt(Number n) : Number {
    return n * 2;
}

function addUp(Number acc, Number x) : Number {
    return acc + x;
}

// ── Sort (default ascending) ──────────────────────────────────────────────────
List:Number nums;
List:Push(5, nums);
List:Push(2, nums);
List:Push(8, nums);
List:Push(1, nums);
List:Sort(nums);
Var:Number n;
List:Get(0, nums, n); Konsol:Print(n);   // 1
List:Get(1, nums, n); Konsol:Print(n);   // 2
List:Get(2, nums, n); Konsol:Print(n);   // 5
List:Get(3, nums, n); Konsol:Print(n);   // 8

// ── Sort (custom descending) ──────────────────────────────────────────────────
List:Sort(descCmp, nums);
List:Get(0, nums, n); Konsol:Print(n);   // 8
List:Get(1, nums, n); Konsol:Print(n);   // 5
List:Get(2, nums, n); Konsol:Print(n);   // 2
List:Get(3, nums, n); Konsol:Print(n);   // 1

// ── Filter ────────────────────────────────────────────────────────────────────
List:Number evens;
List:Filter(isEven, nums, evens);        // source: [8,5,2,1] → evens: [8,2]
Var:Number sz;
List:Size(evens, sz); Konsol:Print(sz);  // 2
List:Get(0, evens, n); Konsol:Print(n); // 8
List:Get(1, evens, n); Konsol:Print(n); // 2

// ── Map ───────────────────────────────────────────────────────────────────────
List:Number mapped;
List:Map(doubleIt, nums, mapped);        // [8,5,2,1] → [16,10,4,2]
List:Get(0, mapped, n); Konsol:Print(n); // 16
List:Get(1, mapped, n); Konsol:Print(n); // 10
List:Get(2, mapped, n); Konsol:Print(n); // 4
List:Get(3, mapped, n); Konsol:Print(n); // 2

// ── Reduce ────────────────────────────────────────────────────────────────────
Var:Number total;
List:Reduce(addUp, 0, nums, total);      // 8+5+2+1 = 16
Konsol:Print(total);                     // 16

// ── String sort ───────────────────────────────────────────────────────────────
List:String words;
List:Push("banana", words);
List:Push("apple", words);
List:Push("cherry", words);
List:Sort(words);
Var:String w;
List:Get(0, words, w); Konsol:Print(w); // apple
List:Get(1, words, w); Konsol:Print(w); // banana
List:Get(2, words, w); Konsol:Print(w); // cherry

Nested collections

// 39_nested_collections.ks — List:Map (list of maps) and List:List (list of lists)

Var:String s;
Var:Number n;

Konsol:Print("=== List of Maps — push / get ===");
Map:New person;
Map:Set("name", "Alice", person);
Map:Set("age", 30, person);

Map:New person2;
Map:Set("name", "Bob", person2);
Map:Set("age", 25, person2);

List:Map people;
List:Push(person, people);
List:Push(person2, people);

List:Size(people, n);
Konsol:Print(n);            // 2

Map:New p;
List:Get(0, people, p);
Map:Get("name", p, s); Konsol:Print(s);   // Alice
Map:Get("age",  p, s); Konsol:Print(s);   // 30

List:Get(1, people, p);
Map:Get("name", p, s); Konsol:Print(s);   // Bob
Map:Get("age",  p, s); Konsol:Print(s);   // 25

Konsol:Print("=== foreach over List:Map ===");
foreach (Map:String entry in people) {
    Map:Get("name", entry, s);
    Konsol:Print(s);        // Alice  Bob
}

Konsol:Print("=== List of Maps — mutation isolation ===");
// Modifying `person` after push does not affect the stored copy
Map:Set("name", "Alicia", person);
List:Get(0, people, p);
Map:Get("name", p, s);
Konsol:Print(s);            // Alice  (copy-on-push, not a reference)

Konsol:Print("=== List of Lists ===");
List:Number row0;
List:Push(1, row0);
List:Push(2, row0);
List:Push(3, row0);

List:Number row1;
List:Push(4, row1);
List:Push(5, row1);
List:Push(6, row1);

List:List matrix;
List:Push(row0, matrix);
List:Push(row1, matrix);

List:Size(matrix, n); Konsol:Print(n);   // 2

List:Number r;
List:Get(0, matrix, r);
List:Get(0, r, n); Konsol:Print(n);      // 1
List:Get(2, r, n); Konsol:Print(n);      // 3

List:Get(1, matrix, r);
List:Get(1, r, n); Konsol:Print(n);      // 5

Konsol:Print("=== pop from List:Map ===");
Map:New popped;
List:Pop(people, popped);
Map:Get("name", popped, s);
Konsol:Print(s);            // Bob
List:Size(people, n);
Konsol:Print(n);            // 1

Konsol:Print("=== Map of Lists ===");
Map:New scores;
List:Number math;
List:Push(90, math);
List:Push(85, math);
Map:SetList("math", math, scores);

List:Number sci;
List:Push(78, sci);
List:Push(92, sci);
Map:SetList("sci", sci, scores);

List:Number got;
Map:GetList("math", scores, got);
List:Get(0, got, n); Konsol:Print(n);     // 90
List:Get(1, got, n); Konsol:Print(n);     // 85

Map:GetList("sci", scores, got);
List:Get(0, got, n); Konsol:Print(n);     // 78

Konsol:Print("=== Map of Maps ===");
Map:New db;
Map:New row1;
Map:Set("name", "Alice", row1);
Map:Set("age", 30, row1);
Map:SetMap("alice", row1, db);

Map:New row2;
Map:Set("name", "Bob", row2);
Map:Set("age", 25, row2);
Map:SetMap("bob", row2, db);

Map:New rec;
Map:GetMap("alice", db, rec);
Map:Get("name", rec, s); Konsol:Print(s);   // Alice
Map:Get("age",  rec, s); Konsol:Print(s);   // 30

Map:GetMap("bob", db, rec);
Map:Get("name", rec, s); Konsol:Print(s);   // Bob

Konsol:Print("=== mutation isolation ===");
Map:Set("name", "Alicia", row1);            // modify original
Map:GetMap("alice", db, rec);
Map:Get("name", rec, s); Konsol:Print(s);   // Alice  (copy-on-set)

Konsol:Print("done");