Added in v2.0.0
Wrap a value into the type constructor.
Signature
export declare const of: <E, A>(a: A) => State<E, A>
Added in v2.0.0
Apply a function to an argument under a type constructor.
Signature
export declare const ap: <E, A>(fa: State<E, A>) => <B>(fab: State<E, (a: A) => B>) => State<E, B>
Added in v2.0.0
map
can be used to turn functions (a: A) => B
into functions (fa: F<A>) => F<B>
whose argument and return types
use the type constructor F
to represent some computational context.
Signature
export declare const map: <A, B>(f: (a: A) => B) => <E>(fa: State<E, A>) => State<E, B>
Added in v2.0.0
Composes computations in sequence, using the return value of one computation to determine the next computation.
Signature
export declare const chain: <E, A, B>(f: (a: A) => State<E, B>) => (ma: State<E, A>) => State<E, B>
Added in v2.0.0
Combine two effectful actions, keeping only the result of the first.
Derivable from Apply
.
Signature
export declare const apFirst: <E, B>(fb: State<E, B>) => <A>(fa: State<E, A>) => State<E, A>
Added in v2.0.0
Combine two effectful actions, keeping only the result of the second.
Derivable from Apply
.
Signature
export declare const apSecond: <E, B>(fb: State<E, B>) => <A>(fa: State<E, A>) => State<E, B>
Added in v2.0.0
Composes computations in sequence, using the return value of one computation to determine the next computation and keeping only the result of the first.
Derivable from Monad
.
Signature
export declare const chainFirst: <E, A, B>(f: (a: A) => State<E, B>) => (ma: State<E, A>) => State<E, A>
Added in v2.0.0
Derivable from Monad
.
Signature
export declare const flatten: <E, A>(mma: State<E, State<E, A>>) => State<E, A>
Added in v2.0.0
Get the current state
Signature
export declare const get: <S>() => State<S, S>
Added in v2.0.0
Get a value which depends on the current state
Signature
export declare const gets: <S, A>(f: (s: S) => A) => State<S, A>
Added in v2.0.0
Modify the state by applying a function to the current state
Signature
export declare const modify: <S>(f: (s: S) => S) => State<S, void>
Added in v2.0.0
Set the state
Signature
export declare const put: <S>(s: S) => State<S, void>
Added in v2.0.0
Signature
export declare const Applicative: Applicative2<'State'>
Added in v2.7.0
Signature
export declare const Functor: Functor2<'State'>
Added in v2.7.0
Signature
export declare const Monad: Monad2<'State'>
Added in v2.7.0
Signature
export declare const URI: 'State'
Added in v2.0.0
Signature
export type URI = typeof URI
Added in v2.0.0
Signature
export declare const state: Monad2<'State'>
Added in v2.0.0
Signature
export interface State<S, A> {
(s: S): [A, S]
}
Added in v2.0.0
Signature
export declare const apS: <A, N extends string, S, B>(
name: Exclude<N, keyof A>,
fb: State<S, B>
) => (fa: State<S, A>) => State<S, { [K in N | keyof A]: K extends keyof A ? A[K] : B }>
Added in v2.8.0
Signature
export declare const bind: <N extends string, A, S, B>(
name: Exclude<N, keyof A>,
f: (a: A) => State<S, B>
) => (fa: State<S, A>) => State<S, { [K in N | keyof A]: K extends keyof A ? A[K] : B }>
Added in v2.8.0
Signature
export declare const bindTo: <N extends string>(name: N) => <S, A>(fa: State<S, A>) => State<S, { [K in N]: A }>
Added in v2.8.0
Run a computation in the State
monad, discarding the final state
Signature
export declare const evaluate: <S>(s: S) => <A>(ma: State<S, A>) => A
Added in v2.8.0
Run a computation in the State
monad discarding the result
Signature
export declare const execute: <S>(s: S) => <A>(ma: State<S, A>) => S
Added in v2.8.0
This function has the same behavior of A.sequence(S.State)
but it's stack safe and optimized
Signature
export declare const sequenceArray: <S, A>(arr: readonly State<S, A>[]) => State<S, readonly A[]>
Example
import * as RA from 'fp-ts/ReadonlyArray'
import { sequenceArray, State } from 'fp-ts/State'
import { pipe, tuple } from 'fp-ts/function'
const add = (n: number): State<number, number> => (s: number) => tuple(n, n + s)
const arr = RA.range(0, 100)
assert.deepStrictEqual(pipe(arr, RA.map(add), sequenceArray)(0), [arr, arr.reduce((p, c) => p + c, 0)])
Added in v2.9.0
This function has the same behavior of A.traverse(S.State)
but it's stack safe and optimized
Signature
export declare const traverseArray: <A, S, B>(f: (a: A) => State<S, B>) => (arr: readonly A[]) => State<S, readonly B[]>
Example
import * as RA from 'fp-ts/ReadonlyArray'
import { traverseArray, State } from 'fp-ts/State'
import { pipe, tuple } from 'fp-ts/function'
const add = (n: number): State<number, number> => (s: number) => tuple(n, n + s)
const arr = RA.range(0, 100)
assert.deepStrictEqual(pipe(arr, traverseArray(add))(0), [arr, arr.reduce((p, c) => p + c, 0)])
Added in v2.9.0
Signature
export declare const traverseArrayWithIndex: <A, S, B>(
f: (index: number, a: A) => State<S, B>
) => (arr: readonly A[]) => State<S, readonly B[]>
Added in v2.9.0
Use evaluate
instead
Signature
export declare const evalState: <S, A>(ma: State<S, A>, s: S) => A
Added in v2.0.0
Use execute
instead
Signature
export declare const execState: <S, A>(ma: State<S, A>, s: S) => S
Added in v2.0.0