Skip to content

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: int

Number of elements.

Array.size ​

ts
readonly size: int

Alias 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): T

Element at index. Negative indices count from the end. Runtime result is undefined when out of range.

Array.includes ​

ts
includes(searchElement: T): boolean

True when searchElement is present (uses === semantics).

Array.indexOf ​

ts
indexOf(searchElement: T, fromIndex?: int): int

Index of the first match, or -1. fromIndex defaults to 0.

Array.lastIndexOf ​

ts
lastIndexOf(searchElement: T): int

Index of the last match, or -1.

Array.find ​

ts
find(callback: Function): any

First element matching callback, or undefined. Callback shape: (value: T, index: int, array: Array<T>): boolean.

Array.findIndex ​

ts
findIndex(callback: Function): int

Index of the first match, or -1. Same callback shape as find.

Array.some ​

ts
some(callback: Function): boolean

True when at least one element passes callback.

Array.every ​

ts
every(callback: Function): boolean

True when every element passes callback.

Array.push ​

ts
push(...items: Array<T>): int

Append items, returns the new length.

Example

ts
const items: int[] = [1, 2];
const length: int = items.push(3, 4);
console.log(length); // 4

Array.pop ​

ts
pop(): T

Remove and return the last element. Runtime result is undefined when empty.

Array.shift ​

ts
shift(): T

Remove and return the first element, shifting the rest down. Runtime result is undefined when empty.

Array.unshift ​

ts
unshift(...items: Array<T>): int

Prepend 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): string

Join 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(): string

Comma-joined string form.

Array.forEach ​

ts
forEach(callback: Function): void

Invoke 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): any

Fold 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(): T

First element. Runtime result is null when empty.

Array.last ​

ts
last(): T

Last element. Runtime result is null when empty.

Array.sample ​

ts
sample(): T

Random element. Runtime result is null when empty. Uses std::rand().

Array.isEmpty ​

ts
isEmpty(): boolean

True when length === 0.

Array.sum ​

ts
sum(): float

Sum of numeric elements. Non-numeric elements are skipped.

Array.avg ​

ts
avg(): float

Mean 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: int

Number of elements: fixed when the array was made.

FixedArray.byteLength ​

ts
readonly byteLength: int

Total size of the elements in bytes.

FixedArray.fill ​

ts
fill(value: T): void

Set 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): void

Copy 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): int

Index of the first element equal to value, or -1.

FixedArray.includes ​

ts
includes(value: T): boolean

True 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: int

Text 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): boolean

True when the text begins with prefix (a string or char array).

FixedArray.equalsIgnoreCase ​

ts
equalsIgnoreCase(other: any): boolean

Equality 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: int

Number of live entries.

Map.length ​

ts
readonly length: int

Alias 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(): void

Remove every entry.

Map.delete ​

ts
delete(key: K): boolean

Remove key. Returns true when an entry was removed.

Map.remove ​

ts
remove(key: K): boolean

Alias of delete.

Map.get ​

ts
get(key: K): V | undefined

Value for key, or undefined.

Example

ts
const ages: Map<string, int> = new Map<string, int>();
ages.set("ada", 36);
console.log(ages.get("ada")); // 36

Map.has ​

ts
has(key: K): boolean

True 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); // 2

Map.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): void

Invoke 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(): any

First inserted value, or null when empty.

Map.last ​

ts
last(): any

Last inserted value, or null when empty.

Map.sample ​

ts
sample(): any

Random value, or null when empty. Uses std::rand().

Map.isEmpty ​

ts
isEmpty(): boolean

True when size === 0.

Map.sum ​

ts
sum(): float

Sum of numeric values. Non-numeric values are skipped.

Map.avg ​

ts
avg(): float

Mean 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: int

Number of live entries.

Set.length ​

ts
readonly length: int

Alias 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(): void

Remove 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); // 2

Set.set ​

ts
set(value: T): Set<T>

Alias of add.

Set.delete ​

ts
delete(value: T): boolean

Remove value. Returns true when a value was removed.

Set.remove ​

ts
remove(value: T): boolean

Alias of delete.

Set.has ​

ts
has(value: T): boolean

True when value is present.

Set.forEach ​

ts
forEach(callback: Function): void

Invoke 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>): boolean

True when every value in this set is also in other.

Set.first ​

ts
first(): any

First inserted value, or null when empty.

Set.last ​

ts
last(): any

Last inserted value, or null when empty.

Set.sample ​

ts
sample(): any

Random value, or null when empty. Uses std::rand().

Set.isEmpty ​

ts
isEmpty(): boolean

True when size === 0.

Set.sum ​

ts
sum(): float

Sum of numeric values. Non-numeric values are skipped.

Set.avg ​

ts
avg(): float

Mean 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 | undefined

Target 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 | undefined

Value 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): boolean

True when key is present.

WeakMap.delete ​

ts
delete(key: K): boolean

Remove 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): boolean

True when value is present.

WeakSet.delete ​

ts
delete(value: T): boolean

Remove 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.