State
State:
object
Defined in: Core/State.ts:27
Type Declaration
Section titled “Type Declaration”apply: <
S,A>(arg) => <B>(state) =>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>(state) => State<S, B>
chain to sequence dependent State transitions.
Example
Section titled “Example”assign
Section titled “assign”assign: <
K,S,A,B>(key,transform) => (state) =>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
transform
Section titled “transform”(value) => State<S, B>
Returns
Section titled “Returns”(state) => State<S, A & { [P in string]: B }>
assignTo to initialize the accumulator record.
Example
Section titled “Example”assignTo
Section titled “assignTo”assignTo: <
K>(key) => <S,A>(state) =>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>(state) => State<S, { [P in string]: A }>
assign to accumulate subsequent State values into the object.
Example
Section titled “Example”chain: <
A,S,B>(transform) => (state) =>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”A
S
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => State<S, B>
Returns
Section titled “Returns”(state) => State<S, B>
map to transform the produced value without changing the state transition.
Example
Section titled “Example”evaluate
Section titled “evaluate”evaluate: <
S>(initialState) => <A>(state) =>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>(state) => A
run to obtain both the value and final state, or execute for only the state.
Example
Section titled “Example”execute
Section titled “execute”execute: <
S>(initialState) => <A>(state) =>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>(state) => S
run to obtain both the value and final state, or evaluate for only the value.
Example
Section titled “Example”focus: <
S,SubState>(lens) => <A>(state) =>State<S,A>
Focuses a State computation on a sub-state using a Lens.
Type Parameters
Section titled “Type Parameters”S
SubState
Section titled “SubState”SubState
Parameters
Section titled “Parameters”Lens<S, SubState>
Returns
Section titled “Returns”<A>(state) => State<S, A>
modify to update state directly without a Lens.
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>
put to replace the state, or modify to transform it with a function.
Example
Section titled “Example”make: <
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>
get to produce the current state instead of a static value.
Example
Section titled “Example”map: <
A,B>(transform) => <S>(state) =>State<S,B> =mapState
Transforms the value produced by a State computation. The state transformation is unchanged.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => B
Returns
Section titled “Returns”<S>(state) => State<S, B>
chain to sequence a computation that produces a new State.
Example
Section titled “Example”modify
Section titled “modify”modify: <
S>(transform) =>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”transform
Section titled “transform”(state) => S
Returns
Section titled “Returns”State<S, undefined>
put to replace the state with a constant value, or get to read it.
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>
get to read the current state, or modify to transform it.
Example
Section titled “Example”run: <
S>(initialState) => <A>(state) => 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>(state) => readonly [A, S]
evaluate to discard the final state, or execute to discard the produced value.
Example
Section titled “Example”tap: <
A>(sideEffect) => <S>(state) =>State<S,A>
Runs a side effect on the produced value without changing the State computation.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(value) => void
Returns
Section titled “Returns”<S>(state) => State<S, A>
map to transform the value instead of executing a side effect.