Skip to content

State

State<S, A> = (s) => readonly [A, S]

Defined in: Core/State.ts:25

A synchronous computation that threads a piece of mutable state S through a pipeline without exposing mutation at call sites.

At runtime a State<S, A> is just a function from an initial state to a pair [value, nextState]. Nothing runs until you supply the initial state with State.run, State.evaluate, or State.execute.

S

A

S

readonly [A, S]

type Counter = number;

const increment: State<Counter, undefined> = State.modify(n => n + 1);
const getCount:  State<Counter, Counter>   = State.get();

const program = pipe(
  increment,
  State.chain(() => increment),
  State.chain(() => getCount),
);

State.run(0)(program); // [2, 2]  — value is 2, final state is 2