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
| Name | Type | Description |
|---|---|---|
ResultType | type | The element type of the new array; supplied first, curried. |
array | Array | The source array. |
callback | function | A 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);Collection.Optional.If
Renders the callback with the payload when the optional is present, and nothing when it is absent.
Parameters
| Name | Type | Description |
|---|---|---|
optional | any | An optional value; present or absent. |
callback | function | Run 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);
});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
| Name | Type | Description |
|---|---|---|
body | function | Declares 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");
});
});Collection.Optional.Switch.Case
One case of a Switch: renders the callback with the payload when this is the first present optional.
Parameters
| Name | Type | Description |
|---|---|---|
optional | any | The optional for this case; present or absent. |
callback | function | Run 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");
});Collection.Union.Tag
A read-only reactive derivation of the active variant name of a union, for display or logic.
Parameters
| Name | Type | Description |
|---|---|---|
union | any | A 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);