Modules · Data · Collection · Array

Collection.Array

An ordered, reactive list of values, all of a single type. Arrays are immutable — operations like Append, Insert and Remove return a new array rather than changing the original. Iterate reactively with Each, build with the Gather/Emit pattern, and transform with Map.

Contents

Types

Functions

Types

Collection.Array

A reactive, ordered list of values, all of a single element type. It is backed by each target's native array; every element carries a stable key, so reactive views built with Collection.Array.Each stay bound to the right element across inserts, removals and reorders.

Generics

NameDescription
ElementTypeThe type of every value held in the array.

Definition

TYPE Collection.Array(ElementType)
The array is a native type: it renders to the reactive array of whichever target you build for.

Example

STATE Collection.Array(Text) names = Collection.Array.Empty
LET Maths.Integer total = Collection.Array.Length names
A reactive array of Text, initialised empty; total stays in step with its length.

Functions

Collection.Array.Append

A new array with the element added to the end.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
elementElementTypeThe value to add.

Returns

Returns Collection.Array(ElementType) — a new array, one longer; the original is unchanged.

Example

LET Collection.Array(Text) grown = Collection.Array.Append names "Grace"
Arrays are immutable, so Append returns a fresh array and leaves names as it was.

Collection.Array.At

The element at an index, falling back to a default when the index is out of range.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.
indexMaths.IntegerThe zero-based position to read.
defaultValueElementTypeReturned when the index is out of range.

Returns

Returns ElementType — the element at the index, or defaultValue when out of range.

Example

LET Text first = Collection.Array.At names 0 "unknown"
Reads the first name, or "unknown" when names is empty.

Collection.Array.AtOptional

The element at an index, as an optional that is absent when the index is out of range.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.
indexMaths.IntegerThe zero-based position to read.

Returns

Returns Collection.Optional(ElementType) — present with the element when the index is in range, absent otherwise.

Example

LET Collection.Optional(Text) third = Collection.Array.AtOptional names 2
Reads position 2; the result is absent if names has fewer than three elements, so it must be unwrapped before use.

Collection.Array.Contains

Yields yes when the array holds an element equal to the given value.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to search.
elementElementTypeThe value to look for.

Returns

Returns Logic.Maybe — yes when an equal element is present, otherwise no.

Example

LET Logic.Maybe known = Collection.Array.Contains names "Ada"
known is yes exactly when "Ada" appears in names.

Collection.Array.Each

Runs a block once for each element, in order.

Generics

NameDescription
ItemTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ItemType)The array to iterate.

Holes

NameReturnsDescription
itemCallbacknothingRun once per element, in order. It opens a reactive branch keyed by the element, so views inside stay bound to that element.
Inputs
NameTypeDescription
itemItemTypeThe current element.

Example

Collection.Array.Each names
  INPUT Text name
  Ui.Content.Text name
Renders one line of text per name. Each iteration is its own reactive branch, so the list reconciles cleanly as names changes.

Collection.Array.Emit

Adds one element to the array being built by the enclosing Gather.

Generics

NameDescription
ElementTypeThe type of element being gathered.

Parameters

NameTypeDescription
elementElementTypeThe value to add to the gathered array.

Example

LET Collection.Array(Maths.Integer) squares = Collection.Array.Gather
  Collection.Array.Emit 1
  Collection.Array.Emit 4
  Collection.Array.Emit 9
Each Emit contributes one element to the array Gather returns. Called outside a Gather it does nothing.

Collection.Array.Empty

An empty array of the element type.

Generics

NameDescription
ElementTypeThe element type of the new array.

Returns

Returns Collection.Array(ElementType) — a new array with no elements.

Example

STATE Collection.Array(Text) names = Collection.Array.Empty
The empty starting point for building an array up with Append or Push.

Collection.Array.First

The first element, falling back to a default when the array is empty.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.
defaultValueElementTypeReturned when the array is empty.

Returns

Returns ElementType — the first element, or defaultValue when empty.

Example

LET Text lead = Collection.Array.First names "nobody"
The first name, or "nobody" when names is empty.

Collection.Array.Gather

Builds an array from the elements a callback emits.

Generics

NameDescription
ElementTypeThe type of element emitted, and of the array built.

Holes

NameReturnsDescription
callbacknothingRun once; each call it makes to Collection.Array.Emit adds one element, in order.

Returns

Returns Collection.Array(ElementType) — the array of everything the callback emitted.

Example

LET Collection.Array(Text) names = Collection.Array.Gather
  Collection.Array.Emit "Ada"
  Collection.Array.Emit "Grace"
Gather opens a collection scope and runs the block; the two Emit calls build ["Ada", "Grace"].

Collection.Array.IndexOf

The index of the first element equal to the given value.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to search.
elementElementTypeThe value to look for.

Returns

Returns Maths.Integer — the zero-based index of the first match, or -1 when there is none.

Example

LET Maths.Integer at = Collection.Array.IndexOf names "Ada"
at is -1 when "Ada" is not present.

Collection.Array.Insert

A new array with the element inserted at an index, shifting later elements along.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
indexMaths.IntegerThe position to insert at.
elementElementTypeThe value to insert.

Returns

Returns Collection.Array(ElementType) — a new array with the element at index.

Example

LET Collection.Array(Text) queued = Collection.Array.Insert names 0 "Lin"
Places "Lin" at the front, shifting every existing name one place along.

Collection.Array.IsEmpty

Yields yes when the array has no elements.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to test.

Returns

Returns Logic.Maybe — yes when the array holds no elements, otherwise no.

Example

STATE Collection.Array(Text) queue = Collection.Array.Empty
LET Logic.Maybe waiting = Collection.Array.IsEmpty queue
An empty queue, so waiting is yes.

Collection.Array.IsNotEmpty

Yields yes when the array holds at least one element.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to test.

Returns

Returns Logic.Maybe — yes when the array holds one or more elements — the negation of IsEmpty.

Example

LET Collection.Array(Text) items = Collection.Array.Append Collection.Array.Empty "one"
LET Logic.Maybe hasItems = Collection.Array.IsNotEmpty items
After one append, items is non-empty, so hasItems is yes.

Collection.Array.Last

The last element, falling back to a default when the array is empty.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.
defaultValueElementTypeReturned when the array is empty.

Returns

Returns ElementType — the last element, or defaultValue when empty.

Example

LET Text latest = Collection.Array.Last names "nobody"
The final name, or "nobody" when names is empty.

Collection.Array.Length

The number of elements in the array.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to measure.

Returns

Returns Maths.Integer — the element count, re-evaluated whenever the array changes.

Example

STATE Collection.Array(Text) tags = Collection.Array.Empty
LET Maths.Integer count = Collection.Array.Length tags
count is reactive — it updates every time tags gains or loses an element.

Collection.Array.Map

A new array holding the result of running a block on each element.

Generics

NameDescription
ElementTypeThe element type of the input array.
ResultTypeThe element type each mapped result produces, and of the new array.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to transform.

Holes

NameReturnsDescription
mapFnResultTypeRun once per element; its result becomes the matching element of the new array.
Inputs
NameTypeDescription
elementElementTypeThe element being transformed.

Returns

Returns Collection.Array(ResultType) — a new array of the mapped results, in the same order.

Example

LET Collection.Array(Maths.Integer) lengths = Collection.Array.Map names
  INPUT Text name
  Text.Length name
Maps each name to its length, giving a Collection.Array(Maths.Integer). Built on Gather, Each and Emit.

Collection.Array.OptionalFirst

The first element as an optional, absent when the array is empty.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.

Returns

Returns Collection.Optional(ElementType) — present with the first element, or absent when empty.

Example

LET Collection.Optional(Text) lead = Collection.Array.OptionalFirst names
An absent result signals an empty array without needing a fallback value.

Collection.Array.OptionalLast

The last element as an optional, absent when the array is empty.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to read.

Returns

Returns Collection.Optional(ElementType) — present with the last element, or absent when empty.

Example

LET Collection.Optional(Text) latest = Collection.Array.OptionalLast names
Absent when the array is empty.

Collection.Array.Pop

A new array with the last element removed.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.

Returns

Returns Collection.Array(ElementType) — a new array without its final element; empty input yields an empty array.

Example

LET Collection.Array(Maths.Integer) rest = Collection.Array.Pop stack
Removes the top of the stack, returning the remainder.

Collection.Array.Push

Appends an element, phrased as a stack push. An alias for Append.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
elementElementTypeThe value to push on the end.

Returns

Returns Collection.Array(ElementType) — a new array with the element on the end.

Example

LET Collection.Array(Maths.Integer) stack = Collection.Array.Push stack 42
Reads as pushing 42 onto a stack; it produces the same new array as Append.

Collection.Array.Remove

A new array with the element at an index removed.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
indexMaths.IntegerThe position to remove.

Returns

Returns Collection.Array(ElementType) — a new array, one shorter; the original is unchanged.

Example

LET Collection.Array(Text) trimmed = Collection.Array.Remove names 0
Drops the first element and returns the shorter array.

Collection.Array.Swap

A new array with the elements at two indices exchanged.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
indexAMaths.IntegerThe first position.
indexBMaths.IntegerThe second position.

Returns

Returns Collection.Array(ElementType) — a new array with the two elements exchanged.

Example

LET Collection.Array(Text) reordered = Collection.Array.Swap names 0 1
Exchanges the first two names.

Collection.Array.Take

A new array of the first count elements.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The starting array.
countMaths.IntegerHow many leading elements to keep.

Returns

Returns Collection.Array(ElementType) — a new array of at most count elements.

Example

LET Collection.Array(Text) topThree = Collection.Array.Take names 3
Keeps the first three names; fewer if names is shorter than three.

Collection.Array.ToString

A text rendering of the array, for debugging and logs.

Generics

NameDescription
ElementTypeThe array's element type.

Parameters

NameTypeDescription
collectionArrayCollection.Array(ElementType)The array to render.

Returns

Returns Text — a human-readable listing of the elements.

Example

LET Text shown = Collection.Array.ToString names
Useful in Console.Log while developing.