Sprites.Array

Reactive helpers over lists. Each reads a list and derives a fact from it, so a length, an item, a slice or an emptiness follows the list. Resize crops or pads a list, and SizeBinding is a two way view of its length. Every reader is a read only calculation; only SizeBinding writes back.

Sprites.Array

Sprites.Array.Append Sprites.Reactive.Value

Sprites.Array.Append(list, item)

A new list with item added at the end. A missing list counts as empty. Each argument may be a constant or a reactive value, so the result follows both.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to add to.
itemAny or Sprites.Reactive.ValueThe value added at the end.

Returns a read-only Sprites.Reactive.Value holding the new list with the item at the end.

const list = Sprites.Reactive.Value(['a', 'b']);
const item = Sprites.Reactive.Value('c');
const longer = Sprites.Array.Append(list, item);
Sprites.Ui.Input.Dropdown(item, () => {
  Sprites.Ui.Input.Option('c', 'c');
  Sprites.Ui.Input.Option('d', 'd');
});
const joined = Sprites.Text.Join(longer, ', ');
Sprites.Ui.Dom.Text(' list = ', joined);
The chosen item added at the end.

Sprites.Array.At Sprites.Reactive.Value

Sprites.Array.At(list, index)

The item of a list at an index, or undefined past the ends. Both may be a constant or a reactive value. It is read only; to change one element build the new list with Sprites.Object.With and set the list.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to read.
indexNumber or Sprites.Reactive.ValueThe position to read.

Returns a read-only Sprites.Reactive.Value holding the item at the index.

const list = Sprites.Reactive.Value(['red', 'green', 'blue']);
const index = Sprites.Reactive.Value(0);
const item = Sprites.Array.At(list, index);
Sprites.Ui.Input.Number(index, 0, 2);
Sprites.Ui.Dom.Text(' item = ', item);
The item at the index, following it.

Sprites.Array.Build Sprites.Reactive.Value

Sprites.Array.Build(count, map)

A list of count items, each the value that map returns for its index. map is a plain function of an index, run for each position. count may be a constant or a reactive value, so the list follows it.

InputTypeDescription
countNumber or Sprites.Reactive.ValueHow many items to build.
mapFunctionA plain function of an index, returning the item at that position.

Returns a read-only Sprites.Reactive.Value holding the built list.

const count = Sprites.Reactive.Value(4);
const squares = Sprites.Array.Build(count, (i) => {
  return i * i;
});
Sprites.Ui.Input.Number(count, 0, 8);
const joined = Sprites.Text.Join(squares, ', ');
Sprites.Ui.Dom.Text(' squares = ', joined);
A list built from its indices, following the count.

Sprites.Array.Emit Void

Sprites.Array.Emit(value)

Emit one value to the nearest Sprites.Array.Gather. Call it inside a Gather build, on its own or within an If, an Each or a Repeat, and the value joins the gathered list in tree order.

InputTypeDescription
valueAny or Sprites.Reactive.ValueThe value to add. A reactive value follows its source; a pending value pends the whole list.

Returns nothing.

const items = Sprites.Array.Gather(() => {
  Sprites.Array.Emit('one');
  Sprites.Array.Emit('two');
});
const joined = Sprites.Text.Join(items, ', ');
Sprites.Ui.Dom.Text(joined);
Two emitted values become a gathered list.

Sprites.Array.Fill Sprites.Reactive.Value

Sprites.Array.Fill(count, value)

A list of count copies of a value. Each argument may be a constant or a reactive value, so the list follows both.

InputTypeDescription
countNumber or Sprites.Reactive.ValueHow many copies to make.
valueAny or Sprites.Reactive.ValueThe value to repeat.

Returns a read-only Sprites.Reactive.Value holding the filled list.

const count = Sprites.Reactive.Value(3);
const dots = Sprites.Array.Fill(count, '.');
Sprites.Ui.Input.Number(count, 0, 8);
const joined = Sprites.Text.Join(dots, '');
Sprites.Ui.Dom.Text(' dots = ', joined);
The same value repeated, following the count.

Sprites.Array.FindIndex Sprites.Reactive.Value

Sprites.Array.FindIndex(list, match, fallback)

The index of the first item that match returns true for, or a fallback when none do. match is a plain function of an item. list and fallback may each be a constant or a reactive value.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to search.
matchFunctionA plain function of an item, true for the item to find.
fallbackAny or Sprites.Reactive.ValueThe value returned when no item matches.

Returns a read-only Sprites.Reactive.Value holding the found index or the fallback.

const list = Sprites.Reactive.Value([3, 7, 4, 9]);
const limit = Sprites.Reactive.Value(5);
const found = Sprites.Array.FindIndex(list, (n) => {
  const max = Sprites.Reactive.Calculate.Get(limit);
  return n > max;
}, -1);
Sprites.Ui.Input.Number(limit, 0, 9);
Sprites.Ui.Dom.Text(' index = ', found);
The first index above the limit, or minus one.

Sprites.Array.FromBits Sprites.Reactive.Value

Sprites.Array.FromBits(bytes, width, height, on, off)

A flat cell array unpacked from a row of packed bits: each byte is one row, the top bit the leftmost pixel, a set bit taking the on value and a clear bit the off value, so a one bit glyph or tile row becomes a width by height cell array. Every argument may be a constant or a reactive value, so the cells follow them.

InputTypeDescription
bytesArray or Sprites.Reactive.ValueThe packed rows, one byte per row, the top bit leftmost.
widthNumber or Sprites.Reactive.ValueThe number of cells across each row.
heightNumber or Sprites.Reactive.ValueThe number of rows.
onAny or Sprites.Reactive.ValueThe value a set bit takes.
offAny or Sprites.Reactive.ValueThe value a clear bit takes.

Returns a read-only Sprites.Reactive.Value holding the width by height cell array.

const bytes = Sprites.Reactive.Value([129, 66, 36, 24]);
const cells = Sprites.Array.FromBits(bytes, 8, 4, '#', '.');
const joined = Sprites.Text.Join(cells, '');
Sprites.Ui.Dom.Text(' cells = ', joined);
Packed bytes unpacked into a grid of hash and dot cells.

Sprites.Array.Gather Sprites.Reactive.Value

Sprites.Array.Gather(build)

Gather the values emitted inside build into one reactive list. Inside build, Sprites.Array.Emit hands a value up; any reactive logic works, so a fixed value, an If that emits only while a condition holds, and an Each that emits one per element may all sit together, and the list follows as the conditions and lists change. It works the same in a lens: the gather runs once and the collected list is the value a Set commits. A pending emitted value, or a pending If or Each condition inside, pends the whole list.

InputTypeDescription
buildFunctionBuilds the emissions. Call Sprites.Array.Emit to add a value, on its own or inside an If, Each or Repeat.

Returns a read-only Sprites.Reactive.Value holding the gathered list.

const extra = Sprites.Reactive.Value(true);
const list = Sprites.Reactive.Value(['a', 'b']);
const gathered = Sprites.Array.Gather(() => {
  Sprites.Array.Emit('start');
  Sprites.Reactive.If(extra, () => {
    Sprites.Array.Emit('extra');
  });
  Sprites.Reactive.Each(list, (item) => {
    Sprites.Array.Emit(item);
  });
  Sprites.Array.Emit('end');
});
Sprites.Ui.Input.Checkbox(extra);
Sprites.Ui.Button.Push(list, 'x', 'Add item');
const joined = Sprites.Text.Join(gathered, ', ');
Sprites.Ui.Dom.Text(' gathered = ', joined);
Toggle the box and add items: the fixed values, the optional one and the per-item ones all follow, in order.

Sprites.Array.IndexOf Sprites.Reactive.Value

Sprites.Array.IndexOf(list, value)

The index of a value in a list, or minus one when it is absent. Both may be a constant or a reactive value, so the result follows them.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to search.
valueAny or Sprites.Reactive.ValueThe value to look for.

Returns a read-only Sprites.Reactive.Value holding the index, or minus one.

const list = Sprites.Reactive.Value(['red', 'green', 'blue']);
const value = Sprites.Reactive.Value('green');
const at = Sprites.Array.IndexOf(list, value);
Sprites.Ui.Input.Dropdown(value, () => {
  Sprites.Ui.Input.Option('red', 'Red');
  Sprites.Ui.Input.Option('green', 'Green');
  Sprites.Ui.Input.Option('blue', 'Blue');
});
Sprites.Ui.Dom.Text(' index = ', at);
The position of the chosen value in the list.

Sprites.Array.InsertAt Sprites.Reactive.Value

Sprites.Array.InsertAt(list, index, item)

A new list with item inserted at index. An index inside the range from zero to the length inclusive lands the item there; an index outside the range keeps the list as it stands. Each argument may be a constant or a reactive value, so the result follows them.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to insert into.
indexNumber or Sprites.Reactive.ValueThe position the item lands at, from zero to the length inclusive.
itemAny or Sprites.Reactive.ValueThe value inserted.

Returns a read-only Sprites.Reactive.Value holding the new list, or the list unchanged when the index is out of range.

const list = Sprites.Reactive.Value(['a', 'b', 'c']);
const index = Sprites.Reactive.Value(1);
const longer = Sprites.Array.InsertAt(list, index, 'x');
Sprites.Ui.Input.Number(index, 0, 3);
const joined = Sprites.Text.Join(longer, ', ');
Sprites.Ui.Dom.Text(' list = ', joined);
The item x inserted at the chosen index.

Sprites.Array.IsEmpty Sprites.Reactive.Value

Sprites.Array.IsEmpty(list)

True when a list holds no items. A missing list counts as empty.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to test.

Returns a read-only Sprites.Reactive.Value, true while the list holds no items.

const list = Sprites.Reactive.Value([]);
const empty = Sprites.Array.IsEmpty(list);
Sprites.Ui.Button.Push(list, 'item', 'Add item');
Sprites.Ui.Button.SetValue(list, [], 'Clear');
Sprites.Ui.Dom.Text(' empty = ', empty);
True until the list holds an item.

Sprites.Array.Length Sprites.Reactive.Value

Sprites.Array.Length(list)

The length of a list. It is read only; for a two way length that resizes the list on set, use SizeBinding.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to measure.

Returns a read-only Sprites.Reactive.Value holding the length.

const list = Sprites.Reactive.Value(['a', 'b']);
const length = Sprites.Array.Length(list);
Sprites.Ui.Button.Push(list, 'x', 'Add');
Sprites.Ui.Dom.Text(' length = ', length);
The count follows the list.

Sprites.Array.Map Sprites.Reactive.Value

Sprites.Array.Map(list, map)

A list with map applied to each item. map is a plain function of an item and its index. list may be a constant or a reactive value, so the result follows it.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to map over.
mapFunctionA plain function of an item and its index, returning the new item.

Returns a read-only Sprites.Reactive.Value holding the mapped list.

const list = Sprites.Reactive.Value([1, 2, 3]);
const doubled = Sprites.Array.Map(list, (n) => {
  return n * 2;
});
Sprites.Ui.Button.Push(list, 4, 'Add 4');
const joined = Sprites.Text.Join(doubled, ', ');
Sprites.Ui.Dom.Text(' doubled = ', joined);
Each item mapped, following the list.

Sprites.Array.RemoveAt Sprites.Reactive.Value

Sprites.Array.RemoveAt(list, index)

A new list with the item at index removed. An index inside the range from zero to the last item removes that item; an index outside the range keeps the list as it stands. Each argument may be a constant or a reactive value, so the result follows them.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to remove from.
indexNumber or Sprites.Reactive.ValueThe position of the item removed, from zero to the last item.

Returns a read-only Sprites.Reactive.Value holding the new list, or the list unchanged when the index is out of range.

const list = Sprites.Reactive.Value(['a', 'b', 'c', 'd']);
const index = Sprites.Reactive.Value(1);
const shorter = Sprites.Array.RemoveAt(list, index);
Sprites.Ui.Input.Number(index, 0, 3);
const joined = Sprites.Text.Join(shorter, ', ');
Sprites.Ui.Dom.Text(' list = ', joined);
The item at the chosen index removed.

Sprites.Array.Resize Sprites.Reactive.Value

Sprites.Array.Resize(list, length, paddingElement)

A list cropped or padded to a length. Positions past the old end take the padding element. It is read only; for a two way length that writes the resized list back, use SizeBinding.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to resize.
lengthNumber or Sprites.Reactive.ValueThe new length.
paddingElementAny or Sprites.Reactive.ValueThe value for positions past the old end.

Returns a read-only Sprites.Reactive.Value holding the resized list.

const list = Sprites.Reactive.Value(['a', 'b', 'c']);
const length = Sprites.Reactive.Value(2);
const sized = Sprites.Array.Resize(list, length, '.');
Sprites.Ui.Input.Number(length, 0, 7);
const joined = Sprites.Text.Join(sized, ', ');
Sprites.Ui.Dom.Text(' sized = ', joined);
Cropped when shorter, padded with a dot when longer.

Sprites.Array.Set Sprites.Reactive.Value

Sprites.Array.Set(list, index, value)

A new list with the item at index replaced by value. An index inside the range keeps every other item as it stands; an index outside the range keeps the whole list as it is. Each argument may be a constant or a reactive value, so the result follows them.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to replace an item in.
indexNumber or Sprites.Reactive.ValueThe position of the item replaced.
valueAny or Sprites.Reactive.ValueThe new value at the index.

Returns a read-only Sprites.Reactive.Value holding the new list, or the list unchanged when the index is out of range.

const list = Sprites.Reactive.Value(['a', 'b', 'c']);
const index = Sprites.Reactive.Value(1);
const updated = Sprites.Array.Set(list, index, 'X');
Sprites.Ui.Input.Number(index, 0, 2);
const joined = Sprites.Text.Join(updated, ', ');
Sprites.Ui.Dom.Text(' list = ', joined);
The item at the chosen index replaced with X.

Sprites.Array.SizeBinding Sprites.Reactive.Value

Sprites.Array.SizeBinding(list, paddingElement)

A two way binding to a list's length. Read, it is the item count; set, it resizes the list, cropping when smaller and padding with the padding element when larger, and writes the new list back. Every set is valid, so the binding is always settable; for a read only count use Length.

InputTypeDescription
listSprites.Reactive.ValueThe list value it reads and writes.
paddingElementAny or Sprites.Reactive.ValueThe value for new positions when growing.

Returns a two-way Sprites.Reactive.Value over the list's length.

const list = Sprites.Reactive.Value(['a', 'b', 'c']);
const size = Sprites.Array.SizeBinding(list, '.');
Sprites.Ui.Input.Number(size, 0, 8);
const joined = Sprites.Text.Join(list, ', ');
Sprites.Ui.Dom.Text(' list = ', joined);
Set the length and the list resizes, padding with a dot.

Sprites.Array.Slice Sprites.Reactive.Value

Sprites.Array.Slice(list, start, end)

A shallow slice of a list, from start up to end. Leave end out for the rest. Each argument may be a constant or a reactive value.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to slice.
startNumber or Sprites.Reactive.ValueThe first index kept.
endNumber or Sprites.Reactive.ValueOptional. The index to stop before.

Returns a read-only Sprites.Reactive.Value holding the slice.

const list = Sprites.Reactive.Value([1, 2, 3, 4, 5]);
const count = Sprites.Reactive.Value(3);
const head = Sprites.Array.Slice(list, 0, count);
Sprites.Ui.Input.Number(count, 0, 5);
const joined = Sprites.Text.Join(head, ', ');
Sprites.Ui.Dom.Text(' head = ', joined);
The first few items, following the count.

Sprites.Array.Some Sprites.Reactive.Value

Sprites.Array.Some(list, match)

True when match returns true for some item. match is a plain function of an item. list may be a constant or a reactive value, so the result follows it.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list to test.
matchFunctionA plain function of an item, true for a matching item.

Returns a read-only Sprites.Reactive.Value, true while some item matches.

const list = Sprites.Reactive.Value([2, 4, 6]);
const hasOdd = Sprites.Array.Some(list, (n) => {
  return n % 2 === 1;
});
Sprites.Ui.Button.Push(list, 7, 'Add 7');
Sprites.Ui.Dom.Text(' hasOdd = ', hasOdd);
True once an odd number is in the list.

Sprites.Array.Choose Sprites.Reactive.Value

Sprites.Array.Choose(mask, on, off)

A list built from a mask list and two values: where a mask element is set the output takes the on value, else the off value. So a one bit glyph row becomes a row of the ink colour where a pixel is set and the paper colour where it is clear. A primitive: one Calculate over a raw map, so a whole cell folds in one step rather than one node per pixel.

InputTypeDescription
maskArray or Sprites.Reactive.ValueThe list whose set elements pick the on value.
onany or Sprites.Reactive.ValueThe value a set element takes.
offany or Sprites.Reactive.ValueThe value a clear element takes.

Returns a read-only Sprites.Reactive.Value holding the chosen list.

Sprites.Array.Lookup Sprites.Reactive.Value

Sprites.Array.Lookup(list, table, indexed)

A list with each element resolved through a table, or kept as it stands. indexed true reads the table at each element, so a slot index becomes the colour there; indexed false returns the element unchanged, a packed colour already. A primitive: one Calculate over a raw map, in one node, so a change recomputes the array once rather than one node per element.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list of elements, indices or values.
tableArray or Sprites.Reactive.ValueThe table an index reads from.
indexedBoolean or Sprites.Reactive.ValueTrue to read the table at each element, false to keep the element.

Returns a read-only Sprites.Reactive.Value holding the resolved list.

Sprites.Array.Concat Sprites.Reactive.Value

Sprites.Array.Concat(first, second)

A new list of the elements of first then the elements of second. A missing list counts as empty, so a join with nothing keeps the other whole. A primitive: one Calculate over the raw join, since concatenation is a base array operation nothing else composes.

InputTypeDescription
firstArray or Sprites.Reactive.ValueThe list whose elements come first.
secondArray or Sprites.Reactive.ValueThe list whose elements follow.

Returns a read-only Sprites.Reactive.Value holding the joined list.

const a = Sprites.Reactive.Value([1, 2]);
const b = Sprites.Reactive.Value([3, 4]);
const joined = Sprites.Array.Concat(a, b);
const length = Sprites.Array.Length(joined);
Sprites.Ui.Dom.Text('length ', length);
The two lists join end to end.

Sprites.Array.KeyBy Sprites.Reactive.Value

Sprites.Array.KeyBy(list, keyName, valueName)

An object mapping each record's key field to its value field, so a list of records reads as a lookup by one of their fields. A later record with the same key wins. A missing list is the empty object. A primitive: one Calculate over the raw fold, since building a lookup is a base operation nothing else composes. Pair it with Sprites.Object.At to read the lookup by a reactive key.

InputTypeDescription
listArray or Sprites.Reactive.ValueThe list of records.
keyNameString or Sprites.Reactive.ValueThe field whose value becomes each entry's key.
valueNameString or Sprites.Reactive.ValueThe field whose value becomes each entry's value.

Returns a read-only Sprites.Reactive.Value holding the lookup object.

const rows = Sprites.Reactive.Value([
  { id: 'a', name: 'Red' },
  { id: 'b', name: 'Green' }
]);
const byId = Sprites.Array.KeyBy(rows, 'id', 'name');
const name = Sprites.Object.At(byId, 'b');
Sprites.Ui.Dom.Text('b → ', name);
The records read as a lookup from id to name.