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:List name Create a list of lists
List:Dictionary name Create a list of dictionaries
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)

outVar must be pre-declared.

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

For List:List and List:Dictionary, Push and Set take an inner collection name rather than a value expression, and Get and Pop write into a pre-declared collection variable rather than a scalar Var. Stored copies are independent - mutating the original after a push does not affect what is already in the list.

// 39_nested_collections.ks - List:Dictionary (list of dictionaries) and List:List (list of lists)

Var:String s;
Var:Number n;

Konsol:Print("=== List of Dictionaries - push / get ===");
Dictionary:New person;
Dictionary:Set("name", "Alice", person);
Dictionary:Set("age", 30, person);

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

List:New people:Dictionary;
List:Push(person, people);
List:Push(person2, people);

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

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

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

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

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

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

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

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

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

List:New r:Number;
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:Dictionary ===");
Dictionary:New popped;
List:Pop(people, popped);
Dictionary:Get("name", popped, s);
Konsol:Print(s);            // Bob
List:Size(people, n);
Konsol:Print(n);            // 1

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

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

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

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

Konsol:Print("=== Dictionary of Dictionaries ===");
Dictionary:New db;
Dictionary:New row1;
Dictionary:Set("name", "Alice", row1);
Dictionary:Set("age", 30, row1);
Dictionary:SetDictionary("alice", row1, db);

Dictionary:New row2;
Dictionary:Set("name", "Bob", row2);
Dictionary:Set("age", 25, row2);
Dictionary:SetDictionary("bob", row2, db);

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

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

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

Konsol:Print("done");


Examples

List

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

Konsol:Print("=== push / size / get ===");
List:New items:Number;
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:New words:String;
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:New nums:Number;
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
}

Sort

// 34_list_sort.ks - List:Sort (ascending, in-place)

// ── Number sort (ascending) ───────────────────────────────────────────────────
List:New nums:Number;
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

// ── String sort (lexicographic) ───────────────────────────────────────────────
List:New words:String;
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