State
State:
object
Defined in: Core/State.ts:27
Type Declaration
Section titled “Type Declaration”ap: <
S,A>(arg) => <B>(fn) =>State<S,B>
Applies a function wrapped in a State to a value wrapped in a State. The function computation runs first; its output state is the input to the argument computation.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”State<S, A>
Returns
Section titled “Returns”<B>(fn) => State<S, B>
Example
Section titled “Example”bind: <
K,S,A,B>(key,f) => (data) =>State<S,A&{ [P in string]: B }>
Evaluates a new State using the current accumulator and attaches the output to a new key.
Type Parameters
Section titled “Type Parameters”K extends string
S
A
B
Parameters
Section titled “Parameters”K
(a) => State<S, B>
Returns
Section titled “Returns”(data) => State<S, A & { [P in string]: B }>
Example
Section titled “Example”bindTo
Section titled “bindTo”bindTo: <
K>(key) => <S,A>(data) =>State<S,{ [P in string]: A }>
Lifts a State value into an accumulator object.
Type Parameters
Section titled “Type Parameters”K extends string
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”<S, A>(data) => State<S, { [P in string]: A }>
Example
Section titled “Example”chain: <
S,A,B>(f) => (st) =>State<S,B> =chainState
Sequences two State computations. The state output of the first is passed as the state input to the second.
Data-last — the first computation is the data being piped.
Type Parameters
Section titled “Type Parameters”S
A
B
Parameters
Section titled “Parameters”(a) => State<S, B>
Returns
Section titled “Returns”(st) => State<S, B>
Example
Section titled “Example”evaluate
Section titled “evaluate”evaluate: <
S>(initialState) => <A>(st) =>A
Runs a State computation with an initial state, returning only the produced value (discarding the final state).
Type Parameters
Section titled “Type Parameters”S
Parameters
Section titled “Parameters”initialState
Section titled “initialState”S
Returns
Section titled “Returns”<A>(st) => A
Example
Section titled “Example”execute
Section titled “execute”execute: <
S>(initialState) => <A>(st) =>S
Runs a State computation with an initial state, returning only the final state (discarding the produced value).
Type Parameters
Section titled “Type Parameters”S
Parameters
Section titled “Parameters”initialState
Section titled “initialState”S
Returns
Section titled “Returns”<A>(st) => S
Example
Section titled “Example”focus: <
S,A>(lens) => <B>(stateOp) =>State<S,B>
Focuses a State computation on a sub-state using a Lens.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Lens<S, A>
Returns
Section titled “Returns”<B>(stateOp) => State<S, B>
Example
Section titled “Example”get: <
S>() =>State<S,S>
Produces the current state as the value, without modifying it.
Type Parameters
Section titled “Type Parameters”S
Returns
Section titled “Returns”State<S, S>
Example
Section titled “Example”gets: <
S,A>(f) =>State<S,A>
Reads a projection of the state without modifying it.
Equivalent to pipe(State.get(), State.map(f)) but more direct.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”(s) => A
Returns
Section titled “Returns”State<S, A>
Example
Section titled “Example”map: <
S,A,B>(f) => (st) =>State<S,B> =mapState
Transforms the value produced by a State computation. The state transformation is unchanged.
Type Parameters
Section titled “Type Parameters”S
A
B
Parameters
Section titled “Parameters”(a) => B
Returns
Section titled “Returns”(st) => State<S, B>
Example
Section titled “Example”modify
Section titled “modify”modify: <
S>(f) =>State<S,undefined>
Applies a function to the current state to produce the next state. Produces no meaningful value.
Type Parameters
Section titled “Type Parameters”S
Parameters
Section titled “Parameters”(s) => S
Returns
Section titled “Returns”State<S, undefined>
Example
Section titled “Example”put: <
S>(newState) =>State<S,undefined>
Replaces the current state with a new value. Produces no meaningful value.
Type Parameters
Section titled “Type Parameters”S
Parameters
Section titled “Parameters”newState
Section titled “newState”S
Returns
Section titled “Returns”State<S, undefined>
Example
Section titled “Example”resolve
Section titled “resolve”resolve: <
S,A>(value) =>State<S,A>
Lifts a pure value into a State computation. The state passes through unchanged.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”State<S, A>
Example
Section titled “Example”run: <
S>(initialState) => <A>(st) => readonly [A,S]
Runs a State computation with an initial state, returning both the produced value and the final state as a pair.
Data-last — the computation is the data being piped.
Type Parameters
Section titled “Type Parameters”S
Parameters
Section titled “Parameters”initialState
Section titled “initialState”S
Returns
Section titled “Returns”<A>(st) => readonly [A, S]
Example
Section titled “Example”tap: <
S,A>(f) => (st) =>State<S,A>
Runs a side effect on the produced value without changing the State computation.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”(a) => void
Returns
Section titled “Returns”(st) => State<S, A>