Collections
Arr[T] and Map[K, V] are available through the prelude. Their implementations live in KataScript’s standard library.
Arrays
Section titled “Arrays”Array literals infer an element type from their values. Elements must have a compatible type.
let values = [10, 20, 30]values.push(40)values[1] = 25
print(values.len) # 4print(values[1]) # 25print(values.get(99).unwrap_or(0)) # 0An empty literal has no element type to infer. Create an empty array explicitly:
let values = Arr[Int].new()values.push(7)print(values.pop().unwrap_or(0))| Operation | Result |
|---|---|
array.len | Number of elements; a field |
array.push(value) | Append one element |
array.pop() | Opt[T] containing the last element, or Non |
array.get(index) | Opt[T]; absent when out of bounds |
array.set(index, value) | Bool indicating whether the index existed |
array[index] | Element; out-of-bounds access stops execution |
array[index] = value | Replace an existing element |
let scores = Map[Str, Int].new()scores["Ada"] = 42scores["Lin"] = 37
print(scores.len()) # 2print(scores["Ada"]) # 42print(scores.get("Sam").unwrap_or(0))Use get when a key may be absent. Indexing assumes it exists and stops execution otherwise. set(key, value) inserts or replaces a value, has(key) tests presence, and del(key) returns whether it removed a key.
Repeated insert/delete cycles have a known corruption bug. Floating-point keys also have equality/hash defects. These examples use string keys and avoid deletion.
Iteration
Section titled “Iteration”let scores = Map[Str, Int].new()scores["Ada"] = 42scores["Lin"] = 37
let total = 0for (_, score) in scores { total = total + score}print(total)Map iteration yields (key, value) tuples. Do not depend on its order; look up a known key sequence when output order matters.
Keep one owner of growth
Section titled “Keep one owner of growth”The current runtime does not give collection aliases coherent independent storage. After let other = values, growing other can invalidate values. Passing or returning a collection also deserves care; these operations are not deep-copy guarantees.
Keep growth on one direct binding, avoid mutation during iteration, and do not rely on generic Drop for cleanup. These are current implementation limits, not a completed ownership model.