Array, Map and Set
The collection types: Array, FixedArray, Map, Set and the weak references.
Always available
No import needed. These names are in scope in every file.
Array class
ts
class Array<T>Indexed, mutable, ordered. The element type T is enforced: a member call binds T to the receiver's element type, so a.push("nope") on an Array<int> is a diagnostic and a.slice(0, 1) answers Array<int>. Element reads answer T, including indexed reads and the single-element methods below. Like an out-of-range indexed read, an empty or out-of-range method result is deliberately typed as T even though the runtime returns undefined from at / pop / shift and null from first / last / sample.
Array.length
ts
readonly length: intNumber of elements.
Array.size
ts
readonly size: intAlias of length. Present so Array, Map, and Set share the collection contract.
new Array()
ts
constructor()
constructor(length: int)
constructor(...items: Array<T>)Empty array.
Array.at
ts
at(index: int): TElement at index. Negative indices count from the end. Runtime result is undefined when out of range.
Array.includes
ts
includes(searchElement: T): booleanTrue when searchElement is present (uses === semantics).
Array.indexOf
ts
indexOf(searchElement: T, fromIndex?: int): intIndex of the first match, or -1. fromIndex defaults to 0.
Array.lastIndexOf
ts
lastIndexOf(searchElement: T): intIndex of the last match, or -1.
Array.find
ts
find(callback: Function): anyFirst element matching callback, or undefined. Callback shape: (value: T, index: int, array: Array<T>): boolean.
Array.findIndex
ts
findIndex(callback: Function): intIndex of the first match, or -1. Same callback shape as find.
Array.some
ts
some(callback: Function): booleanTrue when at least one element passes callback.
Array.every
ts
every(callback: Function): booleanTrue when every element passes callback.
Array.push
ts
push(...items: Array<T>): intAppend items, returns the new length.
Example
ts
const items: int[] = [1, 2];
const length: int = items.push(3, 4);
console.log(length); // 4Array.pop
ts
pop(): TRemove and return the last element. Runtime result is undefined when empty.
Array.shift
ts
shift(): TRemove and return the first element, shifting the rest down. Runtime result is undefined when empty.
Array.unshift
ts
unshift(...items: Array<T>): intPrepend items, returns the new length.
Array.fill
ts
fill(value: T, start?: int, end?: int): Array<T>Fill [start, end) with value in place. Defaults: start=0, end=length.
Array.reverse
ts
reverse(): Array<T>Reverse in place and return the receiver.
Array.sort
ts
sort(compareFn?: Function): Array<T>Sort in place. compareFn(a, b) returns negative / zero / positive; default is lexicographic.
Array.splice
ts
splice(start: int, deleteCount?: int, ...items: Array<T>): Array<T>Remove deleteCount elements at start, insert items in their place, return the removed slice.
Array.concat
ts
concat(...items: Array<T | Array<T>>): Array<T>Shallow copy with items and arrays-of-items appended. This matches JavaScript and TypeScript: when T is an array, an array argument is flattened one level at runtime even though the return is Array<T>.
Array.join
ts
join(separator?: string): stringJoin elements with separator (default ",").
Array.slice
ts
slice(start?: int, end?: int): Array<T>Shallow copy of [start, end). Negative indices count from the end.
Array.flat
ts
flat(): Array<any>One level of nesting flattened. [[1,2],[3]] becomes [1,2,3].
Array.toString
ts
toString(): stringComma-joined string form.
Array.forEach
ts
forEach(callback: Function): voidInvoke callback for each element. Shape: (value: T, index: int, array: Array<T>): void.
Array.map
ts
map(callback: Function): Array<any>New array of the results of callback(value, index, array).
Array.filter
ts
filter(callback: Function): Array<T>New array of the elements callback returned truthy for.
Array.reduce
ts
reduce(callback: Function, initialValue?: any): anyFold with callback(acc, value, index, array). initialValue seeds the accumulator.
Array.entries
ts
entries(): Array<[int, T]>New array of [index, value] pairs.
Array.keys
ts
keys(): Array<int>New array of indices [0..length).
Array.values
ts
values(): Array<T>Shallow copy of the values.
Array.toArray
ts
toArray(): Array<T>Self — present so Array, Map, and Set share the collection contract.
Array.compact
ts
compact(): Array<T>New array with null and undefined removed.
Array.unique
ts
unique(): Array<T>New array with duplicates removed (uses === equality).
Array.shuffle
ts
shuffle(): Array<T>New array with the elements in random order. Uses std::rand().
Array.first
ts
first(): TFirst element. Runtime result is null when empty.
Array.last
ts
last(): TLast element. Runtime result is null when empty.
Array.sample
ts
sample(): TRandom element. Runtime result is null when empty. Uses std::rand().
Array.isEmpty
ts
isEmpty(): booleanTrue when length === 0.
Array.sum
ts
sum(): floatSum of numeric elements. Non-numeric elements are skipped.
Array.avg
ts
avg(): floatMean of numeric elements. NaN when empty.
FixedArray class
ts
class FixedArray<T>FixedArray<T>: an array of a number, boolean, char or struct type whose length is chosen when it is made and never changes. The length is not part of the type: new FixedArray<T>(n) makes n zero elements, and a struct field states its own, name: FixedArray<char>(24). Checks as an array of T; assignment to and from Array<T> converts automatically (a copy), and an out-of-range index or element value raises RangeError. Uint8Array ... Float64Array are aliases of FixedArray<uint8> ... FixedArray<float64> (one type, one runtime kind). Construction and storage rules are in docs/fixed-array.md.
new FixedArray()
ts
constructor(length: int)n zero elements; RangeError for a negative or non-integer n.
FixedArray.length
ts
readonly length: intNumber of elements: fixed when the array was made.
FixedArray.byteLength
ts
readonly byteLength: intTotal size of the elements in bytes.
FixedArray.fill
ts
fill(value: T): voidSet every element to value.
FixedArray.slice
ts
slice(begin?: int, end?: int): FixedArray<T>A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.
FixedArray.set
ts
set(source: any, offset?: int): voidCopy source (an Array or another FixedArray) in starting at offset (default 0). RangeError when it does not fit, or an element is out of range.
FixedArray.indexOf
ts
indexOf(value: T): intIndex of the first element equal to value, or -1.
FixedArray.includes
ts
includes(value: T): booleanTrue when an element equals value.
FixedArray.toArray
ts
toArray(): Array<T>A plain Array<T> copy of the elements. Packed element storage widens to its value type on read.
FixedArray.toSet
ts
toSet(): Set<T>A Set<T> of the elements (duplicates collapse). Packed element storage widens to its value type on read.
FixedArray.textLength
ts
readonly textLength: intText rules, FixedArray<char> only: the text is the elements up to the first zero. length stays the made length; textLength is the text's length.
FixedArray.left
ts
left(n: int): Array<T>The first n characters, as a char array of the same type.
FixedArray.right
ts
right(n: int): Array<T>The last n characters, as a char array of the same type.
FixedArray.mid
ts
mid(start: int, count: int): Array<T>count characters from start, as a char array of the same type.
FixedArray.startsWith
ts
startsWith(prefix: any): booleanTrue when the text begins with prefix (a string or char array).
FixedArray.equalsIgnoreCase
ts
equalsIgnoreCase(other: any): booleanEquality ignoring ASCII case, against a string or char array.
FixedArray.from
ts
static from(source: any): Array<any>The explicit spelling of the automatic Array<T> to FixedArray<T> conversion. It builds the FixedArray the receiving slot declares, so it is only valid where one does: const a: FixedArray<int> = FixedArray.from(arr). source may also be a ByteArray: its bytes are copied at the element's native width, little-endian (a whole number of elements, else RangeError).
Map class
ts
class Map<K, V>Ordered key-value store. Member calls enforce the receiver's K and V. length is exposed as an alias of size for parity with the rest of the collection family.
Map.size
ts
readonly size: intNumber of live entries.
Map.length
ts
readonly length: intAlias of size.
new Map()
ts
constructor(entries?: any)Empty map, or one filled from an iterable of [key, value] pairs (an Array of pairs or another Map).
Map.clear
ts
clear(): voidRemove every entry.
Map.delete
ts
delete(key: K): booleanRemove key. Returns true when an entry was removed.
Map.remove
ts
remove(key: K): booleanAlias of delete.
Map.get
ts
get(key: K): V | undefinedValue for key, or undefined.
Example
ts
const ages: Map<string, int> = new Map<string, int>();
ages.set("ada", 36);
console.log(ages.get("ada")); // 36Map.has
ts
has(key: K): booleanTrue when key is present.
Map.set
ts
set(key: K, value: V): Map<K, V>Set key to value. Returns the receiver.
Example
ts
const ages: Map<string, int> = new Map<string, int>();
ages.set("ada", 36).set("alan", 41);
console.log(ages.size); // 2Map.add
ts
add(key: K, value: V): Map<K, V>Alias of set.
Map.keys
ts
keys(): Array<K>Insertion-ordered keys.
Map.values
ts
values(): Array<V>Insertion-ordered values.
Map.entries
ts
entries(): Array<[K, V]>Insertion-ordered [key, value] pairs.
Map.forEach
ts
forEach(callback: Function): voidInvoke callback for each entry. Shape: (value: V, key: K, map: Map<K, V>): void.
Map.toArray
ts
toArray(): Array<V>Insertion-ordered values. Mirrors the collection contract.
Map.compact
ts
compact(): Map<K, V>New map with entries whose value is null or undefined removed.
Map.first
ts
first(): anyFirst inserted value, or null when empty.
Map.last
ts
last(): anyLast inserted value, or null when empty.
Map.sample
ts
sample(): anyRandom value, or null when empty. Uses std::rand().
Map.isEmpty
ts
isEmpty(): booleanTrue when size === 0.
Map.sum
ts
sum(): floatSum of numeric values. Non-numeric values are skipped.
Map.avg
ts
avg(): floatMean of numeric values. NaN when empty.
Set class
ts
class Set<T>Insertion-ordered, deduplicated value collection. Includes the TKS-specific set algebra extensions (union, intersection, difference, isSubsetOf) registered alongside the standard JS surface.
Set.size
ts
readonly size: intNumber of live entries.
Set.length
ts
readonly length: intAlias of size.
new Set()
ts
constructor(values?: any)Empty set, or one filled from an iterable of values (an Array or another Set).
Set.clear
ts
clear(): voidRemove every value.
Set.add
ts
add(value: T): Set<T>Insert value. Returns the receiver.
Example
ts
const seen: Set<string> = new Set<string>();
seen.add("a").add("b").add("a");
console.log(seen.size); // 2Set.set
ts
set(value: T): Set<T>Alias of add.
Set.delete
ts
delete(value: T): booleanRemove value. Returns true when a value was removed.
Set.remove
ts
remove(value: T): booleanAlias of delete.
Set.has
ts
has(value: T): booleanTrue when value is present.
Set.forEach
ts
forEach(callback: Function): voidInvoke callback for each value. Shape: (value: T, value2: T, set: Set<T>): void.
Set.values
ts
values(): Array<T>Insertion-ordered values.
Set.toArray
ts
toArray(): Array<T>Insertion-ordered values. Mirrors the collection contract.
Set.compact
ts
compact(): Set<T>New set with null and undefined removed.
Set.union
ts
union(other: Set<T>): Set<T>Values present in either set.
Set.intersection
ts
intersection(other: Set<T>): Set<T>Values present in both sets.
Set.difference
ts
difference(other: Set<T>): Set<T>Values present in this set but not in other.
Set.isSubsetOf
ts
isSubsetOf(other: Set<T>): booleanTrue when every value in this set is also in other.
Set.first
ts
first(): anyFirst inserted value, or null when empty.
Set.last
ts
last(): anyLast inserted value, or null when empty.
Set.sample
ts
sample(): anyRandom value, or null when empty. Uses std::rand().
Set.isEmpty
ts
isEmpty(): booleanTrue when size === 0.
Set.sum
ts
sum(): floatSum of numeric values. Non-numeric values are skipped.
Set.avg
ts
avg(): floatMean of numeric values. NaN when empty.
WeakRef class
ts
class WeakRef<T>JavaScript-compatible weak reference to an object. Constructing the reference and successfully dereferencing it keep the target alive until the current job and its Promise microtasks finish.
WeakRef.deref
ts
deref(): T | undefinedTarget while it remains alive, otherwise undefined.
Other members of WeakRef
ts
constructor(target: T)WeakMap class
ts
class WeakMap<K, V>WeakMap.get
ts
get(key: K): V | undefinedValue for key, or undefined when absent or already collected.
WeakMap.set
ts
set(key: K, value: V): WeakMap<K, V>Set key to value. Returns the receiver.
WeakMap.has
ts
has(key: K): booleanTrue when key is present.
WeakMap.delete
ts
delete(key: K): booleanRemove key. Returns true when an entry was removed.
Other members of WeakMap
ts
constructor()WeakSet class
ts
class WeakSet<T>Weakly-referencing set. Members are held weakly; entries disappear when collected. Not iterable and has no size, for the same reason WeakMap has neither.
WeakSet.add
ts
add(value: T): WeakSet<T>Add value. Returns the receiver.
WeakSet.has
ts
has(value: T): booleanTrue when value is present.
WeakSet.delete
ts
delete(value: T): booleanRemove value. Returns true when it was removed.
Other members of WeakSet
ts
constructor()Generated from lang/types/builtin_collections.d.tks in the ThicketScript repository.