Metakit / @doeixd/metakit

Arr

namespace · packages/metakit/src/ops/arr.ts:14

export * as Arr from './ops/arr.js'

Lists: their length (arr.count, arr.isEmpty), what a write makes of one (arr.append, and arr.include and arr.exclude over a set held as a sorted list of scalars), and the questions a Rows node asks of each element (any, every, count with a test, sum, min, max, find, keep).

You write a fold's test as an arrow, called once while the expression is built with a path for the element: Arr.any(state.todos, todo => todo.completed). What is kept is the expression it returns, its reads of the element bound to the fold, so every reader sees the test as nodes. A null list gives null; a test that gives null does not pass.

In the guides

Members

Arr.any

function · packages/metakit/src/ops/arr.ts:176

function any<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
): Expr<EnvOfInput<L> & EnvOfInput<R>, boolean | NullOf<L>>

Whether some element passes test: false over none.

Arr.appendOp

const · packages/metakit/src/ops/arrOps.ts:87

const appendOp = { id, returns, nulls, evaluate }

arr.append(list, element): the list with the element at the end. Called as arr.append(list, fields, ...values) the element is the record of those fields to those values, each field a [name, option] pair: where option is true the element's Schema holds it as an Effect Option, which takes the value as some and null as none. A null list gives null; a null element is appended as it is, since a list may hold null.

Arr.assertCommutativeMonoid

function · packages/metakit/src/ops/arr.ts:249

function assertCommutativeMonoid(op: Node.ForeignOp, caller: string): void

Verify at runtime that op carries the commutative-monoid claims reduce requires.

Arr.at

function · packages/metakit/src/ops/arrIndex.ts:221

function at<
  L extends ExprInput<never, ReadonlyArray<unknown> | null>,
  I extends IndexFor<L>,
>(list: L, index: I): Expr<EnvOfInput<L> & EnvOfInput<I>, ItemAt<L, I>>

The element of list at index, counted from 0: null where the list holds none there (a negative index, or one past the end), and refused for an index that is not a safe integer. Arr.at(state.board, input.cell). Over a tuple a literal index reads that position, typed as it is.

Arr.atOp

const · packages/metakit/src/ops/arrIndex.ts:51

const atOp = { id, returns, nulls, refuses, evaluate }

arr.at(list, index): the element at index of list, counted from 0, read as a path reads one (an Option element is its value or null); null where the list holds no element there (a negative index or one past the end), where either operand is null, or where the element is null.

Arr.bodyOf

function · packages/metakit/src/ops/arr.ts:80

function bodyOf(
  element: Option.Option<SchemaAST.AST>,
  build: (each: Each<unknown>) => unknown,
  name: string,
  binder: Node.Binder,
): Node.Node

A body built from build, called once with a path for an element whose Schema is element, bound to binder. The path refuses to be used once build has returned, so no read of the element escapes its body. A body holds no opaque term, so every reader sees all it asks.

Arr.checkBody

function · packages/metakit/src/ops/arr.ts:110

function checkBody(node: Node.Node, name: string): Node.Node

node, refused where it holds an opaque term, which no body may.

Arr.CommutativeMonoid

interface · packages/metakit/src/ops/arr.ts:236

interface CommutativeMonoid {
  readonly descriptor: Node.ForeignOp & {
    readonly args: readonly [Schema.Top, Schema.Top]
    readonly result: Schema.Top
    readonly claims: {
      readonly commutative: true
      readonly associative: true
      readonly identity: {
        readonly value: unknown
        readonly side: 'both'
      }
    }
  }
}

An operation claiming commutativity, associativity, and a two-sided identity.

Arr.count

function · packages/metakit/src/ops/arr.ts:190

function count<A extends NullableItems>(
  items: A,
): Expr<EnvOfInput<A>, number | NullOf<A>>
function count<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
): Expr<EnvOfInput<L> & EnvOfInput<R>, number | NullOf<L>>

The number of elements (always >= 0), or, with test, of the elements that pass it.

Arr.countOp

const · packages/metakit/src/ops/arrOps.ts:12

const countOp = { id, returns, nulls, evaluate }

Arr.Each

type · packages/metakit/src/ops/arr.ts:59

type Each<E> = PathAccessor<unknown, E>

What a body's arrow is given: a path for the element, typed by the list's element.

Arr.elementAst

function · packages/metakit/src/ops/arr.ts:62

function elementAst(list: Node.Node): Option.Option<SchemaAST.AST>

The Schema of a list's elements, where the list's Schema says it.

Arr.ElementOf

type · packages/metakit/src/ops/arr.ts:49

type ElementOf<L> =
  L extends Expr<never, infer A>
    ? NonNullable<A> extends ReadonlyArray<infer E>
      ? E
      : never
    : L extends ReadonlyArray<infer E>
      ? E
      : never

The element type of a list expression.

Arr.every

function · packages/metakit/src/ops/arr.ts:183

function every<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
): Expr<EnvOfInput<L> & EnvOfInput<R>, boolean | NullOf<L>>

Whether every element passes test: true over none.

Arr.excludeOp

const · packages/metakit/src/ops/arrOps.ts:197

const excludeOp: Node.Op<'arr.exclude'>

Arr.find

function · packages/metakit/src/ops/arr.ts:302

function find<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
): Expr<EnvOfInput<L> & EnvOfInput<R>, ElementOf<L> | null>
function find<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
  V extends ExprInput<never, unknown>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
  value: (each: Each<ElementOf<L>>) => V,
): Expr<
  EnvOfInput<L> & EnvOfInput<R> & EnvOfInput<V>,
  Exclude<V extends ExprInput<never, infer A> ? A : never, null> | null
>

The first element that passes test, or value of it: null where none does.

Arr.has

function · packages/metakit/src/ops/arr.ts:339

function has<
  L extends NullableItems,
  V extends ExprInput<never, number | string | boolean | null>,
>(
  items: L,
  value: V,
): Expr<EnvOfInput<L> & EnvOfInput<V>, boolean | NullOf<L> | NullOf<V>>

True when items (a list of scalars) contains value.

Arr.hasOp

const · packages/metakit/src/ops/arrOps.ts:43

const hasOp = { id, returns, nulls, refuses, evaluate }

Arr.includeOp

const · packages/metakit/src/ops/arrOps.ts:196

const includeOp: Node.Op<'arr.include'>

Arr.IndexFor

type · packages/metakit/src/ops/arrIndex.ts:125

type IndexFor<L> = Expr<never, number | null> | null | LiteralIndex<Held<L>>

The index a list operand takes: a number expression, null, or a number; for a tuple with no rest a literal must be one of its positions, so Arr.at(line, 3) over three positions does not compile.

Arr.isEmpty

function · packages/metakit/src/ops/arr.ts:334

function isEmpty<A extends NullableItems>(
  items: A,
): Expr<EnvOfInput<A>, boolean | NullOf<A>>

True when items has zero elements.

Arr.isEmptyOp

const · packages/metakit/src/ops/arrOps.ts:20

const isEmptyOp = { id, returns, nulls, evaluate }

Arr.ItemAt

type · packages/metakit/src/ops/arrIndex.ts:145

type ItemAt<L, I> = [Named<Held<L>, I>] extends [never]
  ? Held<L> extends ReadonlyArray<infer E>
    ? Read<E> | null
    : never
  : Read<Held<L>[Named<Held<L>, I> & keyof Held<L>]> | NullOf<L> | NullOf<I>

What Arr.at(list, index) reads: at positions of a tuple the index names by its type (a literal, or a literal union Schema.Literals([0, 1, 2])), those positions' types; otherwise any element's, or null.

Arr.keep

function · packages/metakit/src/ops/arr.ts:327

function keep<
  L extends NullableItems,
  R extends ExprInput<never, boolean | null>,
>(
  list: L,
  test: (each: Each<ElementOf<L>>) => R,
): Expr<EnvOfInput<L> & EnvOfInput<R>, ReadonlyArray<ElementOf<L>> | NullOf<L>>

The elements that pass test, in order; the list itself where every one does.

Arr.max

const · packages/metakit/src/ops/arr.ts:297

const max: NumericFold<number | null>

The greatest value of the elements that pass test: null over none, skipping a null value.

Arr.min

const · packages/metakit/src/ops/arr.ts:231

const min: NumericFold<number | null>

The least value of the elements that pass test: null over none, skipping a null value.

Arr.moveOp

const · packages/metakit/src/ops/arrOps.ts:206

const moveOp = { id, returns, nulls, evaluate }

arr.move(list, keyField, keyValue, toIndex): move every element whose elem[keyField] equals keyValue (an Option field reads as its value) as one block, in their order, to toIndex of the list without them (clamped to [0, rest.length]). Returns list unchanged (preserving identity) when no element matches or the block is already there, so move is idempotent.

Arr.nonEmpty

function · packages/metakit/src/ops/arr.ts:349

function nonEmpty<A extends NullableItems>(
  items: A,
): Expr<EnvOfInput<A>, boolean | NullOf<A>>

Derived helper: Bool.not(Arr.isEmpty(items)).

Arr.NullableItems

type · packages/metakit/src/ops/arr.ts:46

type NullableItems = ExprInput<never, ReadonlyArray<unknown> | null>

An array operand; where it may be null, so may the result.

Arr.PositionFor

type · packages/metakit/src/ops/arrIndex.ts:131

type PositionFor<T> = Expr<never, unknown> | LiteralIndex<T>

What Transition.setAt's index may be beside an expression: a literal index of T.

Arr.reduce

function · packages/metakit/src/ops/arr.ts:268

function reduce<
  L extends NullableItems,
  O extends CommutativeMonoid,
  V extends ExprInput<
    never,
    Schema.Schema.Type<O['descriptor']['result']> | null
  >,
  T extends ExprInput<never, boolean | null> = true,
>(
  list: L,
  op: O,
  value: (each: Each<ElementOf<L>>) => V,
  test?: (each: Each<ElementOf<L>>) => T,
): Expr<
  EnvOfInput<L> & EnvOfInput<V> & EnvOfInput<T>,
  Schema.Schema.Type<O['descriptor']['result']> | NullOf<L>
>

Fold value over the elements of list that pass test using a declared commutative-monoid operation op: returns op's identity over no elements and skips null contributions.

Arr.rowsOver

function · packages/metakit/src/ops/arr.ts:134

function rowsOver(
  fold: Node.Fold,
  list: Node.Node,
  body: {
    readonly where?: Node.Node
    readonly value?: Node.Node
    readonly set?: ReadonlyArray<readonly [string, Node.Node]>
    readonly op?: Node.ForeignOp
    readonly binder: Node.Binder
  },
): Node.Node

A Rows node over list.

Arr.sum

const · packages/metakit/src/ops/arr.ts:228

const sum: NumericFold<number>

The total of value over the elements that pass test (every one without it): 0 over none; a null value adds nothing; an integer total past ±(2^53−1) is refused.

Arr.ValueAt

type · packages/metakit/src/ops/arrIndex.ts:156

type ValueAt<T, I> =
  IsList<T> extends true
    ? T extends ReadonlyArray<infer E>
      ? E
      : never
    : [Named<T, I>] extends [never]
      ? [LiteralOf<I>] extends [never]
        ? EveryOf<T, keyof T & `${number}`> &
            ([RestOf<T>] extends [never] ? unknown : RestOf<T>)
        : RestOf<T>
      : EveryOf<T, `${Named<T, I>}` & keyof T>

What Arr.with(list, index, value) and Transition.setAt take as the value: for a list, an element; for a tuple, a value every position the index may name takes (the named ones, or all of them for a computed index).

Arr.with

function · packages/metakit/src/ops/arrIndex.ts:260

function with<L extends ExprInput<never, ReadonlyArray<unknown> | null>, I extends IndexFor<L>, V extends ExprInput<never, NoInferEach<ValueAt<Held<L>, I>>>>(list: L, index: I, value: V): Expr<EnvOfInput<L> & EnvOfInput<I> & EnvOfInput<V>, Held<L> | NullOf<L> | NullOf<I>>;

list with its element at index replaced by value: the list a Transition.setAt of the same three leaves, for a guard or a rule over the state after it. Refused where index names no element of the list, or is not a safe integer; null where the list or the index is. Over a tuple the value must fit every position the index may name.

Arr.within

function · packages/metakit/src/ops/arr.ts:95

function within<A>(
  binder: Node.Binder,
  element: Option.Option<SchemaAST.AST>,
  name: string,
  run: (each: Each<unknown>) => A,
): A

What run gives, handed the accessor of binder's element, typed by element, which refuses to be used once run has returned.

Arr.withOp

const · packages/metakit/src/ops/arrIndex.ts:74

const withOp = { id, returns, nulls, evaluate }

arr.with(list, index, value): the list with its element at index replaced by value (which may be null: a list may hold one), the list itself where that element is value already. Null where the list or the index is; refused where the index names no element of the list, since no element is there to replace.