Modules · Data

Collection

The reactive way to work with arrays, optionals and union tags. The list type and its builders, Element, Elements, Empty and Each, have their own page: see Collection.Array. This page covers the rest of the surface: Collection.Array.Map, which transforms an array through a reactive callback while keeping each element's key; Collection.Optional.If and Collection.Optional.Switch, which render from an optional or branch a tagged union without a string tag; and Collection.Union.Tag, a read-only view of a union's active variant name.

Contents

Functions

Functions

Collection.Array.Map

Maps each element of a reactive array through a reactive callback, collecting the returned values into a new keyed array.

Parameters

NameTypeDescription
ResultTypetypeThe element type of the new array; supplied first, curried.
arrayArrayThe source array.
callbackfunctionA reactive callback, inline or the name of a reactive function; run once per element, its returned value becomes the new element.

Returns

A new reactive Array of ResultType. Each mapped element's key derives from the source element's key, so a view built with Each re-renders only the values that changed.

Example

const nums = Collection.Array.From(1, 2, 3);
const doubled = Collection.Array.Map(Number)(nums, (n) => n * 2);
Mapping each element through a reactive callback

Collection.Optional.If

Renders the callback with the payload when the optional is present, and nothing when it is absent.

Parameters

NameTypeDescription
optionalanyAn optional value; present or absent.
callbackfunctionRun with the present payload as its single input; build the content for the present case here.

Example

Collection.Optional.If(user.name, (name) => {
  Content.Text(name);
});
Rendering only when the optional is present

Collection.Optional.Switch

A one-of over optionals: the first present case renders. This is how you branch a tagged union without a string tag.

Parameters

NameTypeDescription
bodyfunctionDeclares the cases with Collection.Optional.Switch.Case; the first case whose optional is present renders, the rest render nothing.

Example

Collection.Optional.Switch(() => {
  Collection.Optional.Switch.Case(shape.circle, (circle) => {
    Content.Text("a circle");
  });
  Collection.Optional.Switch.Case(shape.square, (square) => {
    Content.Text("a square");
  });
});
Branching a union by its present variant

Collection.Optional.Switch.Case

One case of a Switch: renders the callback with the payload when this is the first present optional.

Parameters

NameTypeDescription
optionalanyThe optional for this case; present or absent.
callbackfunctionRun with the present payload as its single input when this case is the first present one.

Example

Collection.Optional.Switch.Case(shape.circle, (circle) => {
  Content.Text("a circle");
});
One case, used inside a Switch body

Collection.Union.Tag

A read-only reactive derivation of the active variant name of a union, for display or logic.

Parameters

NameTypeDescription
unionanyA union value; the one present variant names the tag.

Returns

A reactive string: the active variant name. Read-only, not two-way; to switch variant, build the new option's value with an explicit reverse.

Example

const tag = Collection.Union.Tag(shape);
Content.Text(tag);
Showing which variant is active