@b-jones-rfd/func-help - v0.7.1
    Preparing search index...

    Class State<S, A>

    A computation that takes a state of type S and yields a value of type A together with the next state.

    Nothing runs until run(), eval() or exec() is called with an initial state, so a State can be built once and run against many starting states.

    A throw in a caller-supplied function is thrown from run(), eval() or exec(), not from the call that built the State.

    import { State } from '@b-jones-rfd/func-help'

    const increment = State.modify<number>((n) => n + 1)
    const getCount = State.get<number>()

    const program = increment
    .bind(() => increment)
    .bind(() => getCount)

    const [result, finalState] = program.run(0)
    // result === 2, finalState === 2

    Type Parameters

    • S

      The state threaded through the computation.

    • A

      The value the computation yields.

    Implements

    Index
    • Let yield* bind this State inside a do block: yields this State and returns the value the block is resumed with.

      It exists for yield*, not for spreading: [...state] is a one-element array holding this State itself, and does not run it.

      Returns Generator<State<S, A>, A, unknown>

      A generator that yields this State once.

      import { State } from '@b-jones-rfd/func-help'

      State.do(function* () {
      const n = yield* State.get<number>() // calls this method
      return n * 2
      }).eval(3) // 6
    • Sequence a step that depends on this step's value, passing the state from one to the next.

      Haskell's >>=. If f throws, the error is thrown from run(), eval() or exec().

      Type Parameters

      • B

        The new value type.

      Parameters

      • f: (a: A) => State<S, B>

        The next step, given this step's value.

      Returns State<S, B>

      A State that runs this step, then the State f returns.

      import { State } from '@b-jones-rfd/func-help'

      const double = State.get<number>().bind((n) => State.put(n * 2))
      double.exec(4) // 8
    • Run the computation from an initial state and return only the value.

      Haskell's evalState.

      Parameters

      • initial: S

        The state to start from.

      Returns A

      The value the computation yields; the final state is dropped.

      TypeError When a bind step returns a monad of another kind instead of a State. The types rule this out; only an untyped Do.run/Do.with or a cast can reach it.

      import { State } from '@b-jones-rfd/func-help'

      State.gets<string[], number>((xs) => xs.length).eval(['a', 'b']) // 2
    • Run the computation from an initial state and return only the final state.

      Haskell's execState.

      Parameters

      • initial: S

        The state to start from.

      Returns S

      The final state; the value is dropped.

      TypeError When a bind step returns a monad of another kind instead of a State. The types rule this out; only an untyped Do.run/Do.with or a cast can reach it.

      import { State } from '@b-jones-rfd/func-help'

      State.modify<number>((n) => n * 10).exec(3) // 30
    • Transform the value the computation yields; the state is untouched.

      Haskell's fmap. If f throws, the error is thrown from run(), eval() or exec().

      Type Parameters

      • B

        The new value type.

      Parameters

      • f: (a: A) => B

        The function to apply to the value.

      Returns State<S, B>

      A State that yields f(value) with the same state.

      import { State } from '@b-jones-rfd/func-help'

      State.get<number>().map((n) => `count: ${n}`).eval(3) // 'count: 3'
    • Run the computation from an initial state and return the value and the final state.

      Haskell's runState; the pair is [result, state], the same order as Haskell's (a, s).

      Parameters

      • initial: S

        The state to start from.

      Returns StatePair<S, A>

      The tuple [result, finalState].

      TypeError When a bind step returns a monad of another kind instead of a State. The types rule this out; only an untyped Do.run/Do.with or a cast can reach it.

      import { State } from '@b-jones-rfd/func-help'

      State.gets<number, boolean>((n) => n > 0).run(2) // [true, 2]
    • Return a function that runs the computation from a given state, for callers that want to run it later or pass it where a plain function is expected.

      Haskell's runState used unapplied, as a function s -> (a, s).

      Returns (state: S) => StatePair<S, A>

      A function from an initial state to [result, finalState].

      TypeError When a bind step returns a monad of another kind instead of a State. The types rule this out; only an untyped Do.run/Do.with or a cast can reach it.

      import { State } from '@b-jones-rfd/func-help'

      const step = State.modify<number>((n) => n + 1).toFunction()
      step(0) // [undefined, 1]
    • Build a State from a do block: Haskell's do notation, with x <- m written const x = yield* m.

      Each yield* m runs m against the state the previous step left and binds its value, typed with no annotation. The state type S is inferred from the States the block yields, and every one must share it. Nothing in the block runs until run(), eval() or exec(), and every run starts the block afresh. A throw in the block is thrown from that call. A plain yield (without *) binds unknown; use yield*.

      Type Parameters

      • S

        The state type; inferred from the yielded States.

      • A

        The value the block returns.

      Parameters

      • block: () => Generator<State<S, unknown>, A, unknown>

        A generator function that yield*s States and returns the result.

      Returns State<S, A>

      A State that runs the block and yields what it returns.

      import { State } from '@b-jones-rfd/func-help'

      const counter = State.do(function* () {
      yield* State.modify<number>((n) => n + 1)
      const count = yield* State.get<number>() // number
      return `count: ${count}`
      }) // State<number, string>

      counter.run(1) // ['count: 2', 2]
    • Read the current state as the value, without changing it.

      Haskell's get.

      Type Parameters

      • S

        The state type.

      Returns State<S, S>

      A State that yields the current state.

      import { State } from '@b-jones-rfd/func-help'

      State.get<string>().eval('hello') // 'hello'
    • Read a value derived from the state, without changing it.

      Haskell's gets. If f throws, the error is thrown from run(), eval() or exec().

      Type Parameters

      • S

        The state type.

      • A

        The derived value type.

      Parameters

      • f: (s: S) => A

        The function that derives the value from the state.

      Returns State<S, A>

      A State that yields f(state).

      import { State } from '@b-jones-rfd/func-help'

      type User = { name: string; age: number }
      State.gets((u: User) => u.name).eval({ name: 'Ada', age: 36 }) // 'Ada'
    • Replace the state with the result of applying f to it.

      Haskell's modify. If f throws, the error is thrown from run(), eval() or exec().

      Type Parameters

      • S

        The state type.

      Parameters

      • f: (s: S) => S

        The function from the current state to the next.

      Returns State<S, void>

      A State that sets the state to f(state).

      import { State } from '@b-jones-rfd/func-help'

      State.modify<string[]>((xs) => [...xs, 'b']).exec(['a']) // ['a', 'b']
    • Lift a value into State, leaving the state unchanged.

      Haskell's pure for State s.

      Type Parameters

      • S

        The state type.

      • A

        The value type.

      Parameters

      • value: A

        The value the computation yields.

      Returns State<S, A>

      A State that yields value and passes the state through.

      import { State } from '@b-jones-rfd/func-help'

      State.of<number, string>('ok').run(5) // ['ok', 5]
    • Replace the current state.

      Haskell's put; the value is undefined, standing in for Haskell's ().

      Type Parameters

      • S

        The state type.

      Parameters

      • newState: S

        The state to store.

      Returns State<S, void>

      A State that sets the state to newState.

      import { State } from '@b-jones-rfd/func-help'

      State.put(42).exec(0) // 42